it is an example of running Node.js script with every certain period(cron and non-cron job)
- We can provide the full path to node
/usr/local/bin/node
in your cron job like:30 6 1 * * /usr/local/bin/node /home/steve/example/script.js
- Or making a script with the command, and then adding that to cron:
#!/usr/bin/env sh node /home/campaigns/reporting/UNIT_TESTS/testCron.js > /home/campaigns/reporting/UNIT_TESTS/cron.log
- The problem with the two above methods is messing up the path. The command is in
absolute path
, but the Node.js script usesrelative path
to import/require other modules. It causes the error of file not found. So we need to execute the cron under the directory of Node.js script, which contains all the modules which will be used.
This lib is used to keep the cron-job alive, which triggers the node script at a certain time.
$ npm install --save node-cron
Import node-cron and schedule a task:
var cron = require('node-cron');
cron.schedule('* * * * *', function(){
console.log('running a task every minute');
});
- To run script :
$ node script1.js
- And script:
$ npm run script -- PeterGood
child_help.js
is an amazing Node.js script from mout, which helps to manage multiple linux commands.
Start a Daemon, and run
$ node cronNodeScript
Execute script every 1 min
//execute every 1 min
cron.schedule('*/1 * * * *', function(){
....
});
Execute $ node script1.js
and npm run script -- PeterGood
every 1 min
//execute every 1 min
cron.schedule('*/1 * * * *', function(){
var shell = require('./child_helper');
var commandList = [
"node script1.js",
"npm run script -- PeterGood"
]
shell.series(commandList , function(err){
// console.log('executed many commands in a row');
console.log('done')
});
});