Run background script

3

I'm doing a process with nodejs and I need to run say at every hour without needing to continually do something like node app.js . The script could have for example a console.log('ejecutar a cada hora') . Now, I was thinking about doing:

setInterval(function(){
  console.log('ejecutar a cada hora');
}, 3600);

and just run it with pm2 , but I do not like the idea of using setInterval Any alternative solution?

I'm looking for something like:

pm2 app.js --interval 3600

It is not necessary that it be with pm2 although it would be ideal.

    
asked by learnercys 15.01.2016 в 21:23
source

2 answers

5

Why would not you use setInterval ? In the documentation of pm2 use it, the only caution they indicate is to use clearInterval for avoid leaving the process hanging.

var argParser = require('minimist')

var argv = argParser(process.argv.slice(2))
var period = argv.interval || 3600

var interval = setInterval(function () {
  console.log('ejecutar a cada hora')
}, period)

process.on('SIGINT', function() {
  clearInterval(interval)
})

Minimist parse the arguments so you can configure how often the process runs.

    
answered by 16.01.2016 / 03:26
source
1

Maybe monit could help you:

And you set the task as follows

check program app-js with path "/usr/bin/node /path/de/tu/script.js"
   if status != 0 then alert #verifica que no se este ejecutando
     every "60 * * * *"
    
answered by 15.01.2016 в 23:44