Task scheduling in Node.js using node-cron
We know cron is a task scheduler which is used in Unix based operating systems. It will execute something on a schedule using its syntax. In Node.js we have node-cron which is a module in pure JavaScript and it is based on GNU crontab. Let’s get started with task scheduling using node-cron.
Install node-cron using npm.
$ npm install --save node-cron
Now we import node-cron and schedule the task as given below:
var cron = require('node-cron');
cron.schedule('* * * * *', () => {
console.log('running a task every minute');
});
We can specify cron values using the allowed ranges or fields. Following are the ranges.
Seconds: 0-59
Minutes: 0-59
Hours: 0-23
Day of Month: 1-31
Months: 0-11 (Jan-Dec)
Day of Week: 0-6 (Sun-Sat)
Comments
0 comments
Please Sign in or Create an account to Post Comments