Where cron jobs come in?
Ever wanted to do specific things on your application server at certain times without having to physically run them yourself. Trust me there always comes a time in app development where you will want to automate certain aspects of our project. For example, run some scripts to import data into our system periodically from an outside database. This is where Cron jobs come in.What is cron?
Cron is a scheduler utility provided by Linux that allows you to schedule a job that can execute a command, run a script or perform a specific operation periodically.How to set up a cron job in Node JS?
Cron jobs in Node.js can be set up using an external module known as node-cron. This module is based on GNU crontab.1. Prerequisites- Node installed on your machine
- NPM installed on your machine
- Basic Knowledge of JavaScript
- Create Node Application into your system
- Install Node packages like express
- Create Backend Server in index.js file using the express package
- Then Listen to your Application with some random port e.g 4001
npm install node-cron --saveconst cron = require('node-cron');- Interval or the time at which the cron job is needed to run
- The callback function contains the code to be executed
- scheduled: accepts a boolean value. If set to false then the cron won't start automatically
- timezone: accepts the timezone and will interpret the time set in the first parameter of schedule method accordingly
const cron = require('node-cron');
var task = cron.schedule('* * * * *', () => {
console.log('Printing this line every minute in the terminal');
});┌──────────────── second (optional) (Valid range 0-59)
| ┌────────────── minute (Valid range: 0-59)
| | ┌──────────── hour (valid range: 0-23)
| | | ┌────────── day of the month (Valid range: 1-31)
| | | | ┌──────── month (Valid range: 1-12)
| | | | | ┌────── day of the week (valid range: 0-7)
| | | | | |
| | | | | |
* * * * * *6. Example of Cron Job with TimezoneThe below code will print the line in the server every day when the time at "Asia/Mumbai" timezone is 08:20 Hours :
const cron = require('node-cron');
// cron won’t start automatically
var task = cron.schedule('20 08 * * *', () => {
console.log('Printing this line every day at 1750 Hours London Time.');
},
{
scheduled: false,
timezone: "Asia/Mumbai"
});
// start method is called to start the above defined cron job
task.start();
// stop method is called to stop already started cron job
task.stop();
// destroy method is called to stop and destroy already started cron job, after destroy you have to reinitialize the task as it is destroyed.
task.destroy();cron.schedule('* * * * * *', () => {
// code
});cron.schedule('* * * * *', () => {
// code
});cron.schedule('0 * * * *', () => {
// code
});cron.schedule('30 08 10 * *', () => {
// code
});cron.schedule('10-20 * * * *', () => {
// code
});cron.schedule('20,50 * * * *', () => {
// code
});cron.schedule('*/10 * * * *', () => {
// code
});cron.schedule('10 08 * 2 1', () => {
// code
});Conclusion
- We saw how easy it is to set up a cron job in Nodejs and to use it in the Express server by using the cron-node library.
- We saw different types of examples that help us in creating our cron job logic based on our requirements.
- Here is a link that will be helpful in building node-cron schedule parameters : https://crontab.guru/#10_08_*_2_1


