To add cron job in node.js, you can use npm package mentioned below.
node-cron
Node Cron
Node-cron is a small task scheduler written in pure JavaScript and made specifically for the node.js programming language that has been developed off of GNU crontab. With this module you can schedule tasks at certain times using the full syntax of cron.
Let’s Start
To install the module the following command is executed from the terminal
npm install --save node-cron
Import node-cron and schedule a task:
var cron = require('node-cron');
cron.schedule('* * * * *', () => {
console.log('running a task every minute');
});
how to schedule task using different intervals? Please check below.
This is a quick reference to cron syntax and also shows the options supported by node-cron.
Allowed fields
# ┌────────────── second (optional)
# │ ┌──────────── minute
# │ │ ┌────────── hour
# │ │ │ ┌──────── day of month
# │ │ │ │ ┌────── month
# │ │ │ │ │ ┌──── day of week
# │ │ │ │ │ │
# │ │ │ │ │ │
# * * * * * *
Allowed values
Field Value
second 0-59
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names)
day of week 0-7 (or names, 0 or 7 are Sunday)
Using multiples values
You may use multiples values separated by comma:
var cron = require('node-cron');
cron.schedule('1,2,4,5 * * * *', () => {
console.log('running every minute 1, 2, 4 and 5');
});
Using ranges
You may also define a range of values:
var cron = require('node-cron');
cron.schedule('1-5 * * * *', () => {
console.log('running every minute to 1 from 5');
});
Using step values
Step values can be used in conjunction with ranges, following a range with ‘/’ and a number. e.g: 1-10/2
that is the same as 2,4,6,8,10
. Steps are also permitted after an asterisk, so if you want to say “every two minutes”, just use */2
.
var cron = require('node-cron');
cron.schedule('*/2 * * * *', () => {
console.log('running a task every two minutes');
});
Using names
For month and week day you also may use names or short names. e.g:
var cron = require('node-cron');
cron.schedule('* * * January,September Sunday', () => {
console.log('running on Sundays of January and September');
});
Or with short names:
var cron = require('node-cron');
cron.schedule('* * * Jan,Sep Sun', () => {
console.log('running on Sundays of January and September');
});
Scheduled Examples
- Starts the scheduled task.
var cron = require('node-cron');
var task = cron.schedule('* * * * *', () => {
console.log('stopped task');
}, {
scheduled: false // 'False is used to run the task for single time.'
});
task.start();
- Stops the scheduled task.
var cron = require('node-cron');
var task = cron.schedule('* * * * *', () => {
console.log('will execute every minute until stopped');
});
// Start the task.
task.start();
// To stop the task, you will need to use below method else the task will execute every minute until stopped.
task.stop();
//You can write this task stop method wherever you want to execute(like: when your logic works, you can then stop the task.)
Source : npmjs.com