How can I run my node js script automatically using scheduler on server
1. If you want to run your node process continuously and want to run only particular task:
Use node-schedule or node-cron packages to run your code block at desired time or interval.
i.node-schedule
var schedule = require('node-schedule');
var j = schedule.scheduleJob('*/30 * * * * ', function(){
console.log('The answer to life, the universe, and everything!');
});
ii.node-cron
var cron = require('node-cron');
cron.schedule('*/30 * * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
2. If you want to run only single node script:
You can use Linux crontab to execute your script at desired time
crontab -e
and add following entry
*/30 * * * * /usr/local/bin/node /home/ridham/example/script.js
This will execute /home/ridham/example/script.js
every 30 minutes. and always give full qualified path here.
You have to give crontime in any of the following. you can learn about crontime here