Contents hide 1) What is a cron job? 2) Installing node-cron and express package 3) Case 1 β Scheduling a task using node-cron 4) Case 2 β Use node-cron to back up databases 5) More use case for cronjobs 6) Conclusion This tutorial will see how we can Schedule A Cron Job In NodeJS Using the node-cron module. What is a cron job? A cron job is the event schedular that runs a job as per the given time interval. So it executes a file linked which is linked to that particular job, it runs the code snippet of it and this configuration is set on the server. But in NodeJS we can actually set cron job within the application code using the node-cron module. Installing node-cron and express package Let us start with creating a new application i.e. a new folder for your project. # Create a folder mkdir cron-jobs-node # Go to a folder cd cron-jobs-node # Initialize it, which creates a package.json file that you will use to track dependencies npm init -y # Add the express web framework and node-cron module by running the following command npm install express node-cron As we have installed the basic required dependencies. Letβs build the server. Case 1 β Scheduling a task using node-cron Now, we can build the server and also will use node-cron to schedule a task that will run every minute. so letβs first create an index.js file nano index.js Now, letβs import the required packages and create our scheduler. index.js // IMPORTING PACKAGES const express = require('express'); const cron = require('node-cron'); app = express(); // Schedule job to be run on the server. cron.schedule('* * * * *', () => { console.log('running a job every minute'); }); // SERVE THE APP app.listen(3000); Note: cron.schedule('* * * * *', () => {...}) The asterisks are part of the crontab syntax to represent different units of time. A single asterisk behaves like a wildcard. Meaning the task should be run should for every instance of that unit of time. Five asterisks ('* * * * *') represents the crontab default of running every minute. Example: βAt 22:00 on every day-of-week from Monday through Friday.β Now run the server and letβs see what we get in result: node index.js # Output running a job every minute running a job every minute running a job every minute ... You have an example job running every minute. You can stop the server with CTRL+C. Case 2 β Use node-cron to back up databases Consider a case where your project needs a backup of the database every day at some timestamp. This event is basically to prevent any corrupted data which might break the database, so in that case, a backup of a database can be very helpful and it can be less damage for your business. So letβs assume we have an SQLite DB installed and running on our environment. The Shell command for making a database backup may resemble this: sqlite3 database.sqlite .dump > data_dump.sql So there a package available called shelljs which runs the shell command directly from the code. npm install shelljs --save Now inside the schedule method, we can run our shell script to dump the database and make a copy of it. // IMPORTING PACKAGES const cron = require('node-cron'); const shell = require('shelljs'); const express = require('express'); app = express(); // Backup a database at 11:59 PM every day. cron.schedule('59 23 * * *', function() { console.log('---------------------'); console.log('Running Cron Job'); if (shell.exec('sqlite3 database.sqlite .dump > data_dump.sql').code !== 0) { shell.exit(1); } else { shell.echo('Database backup complete'); } }); // SERVE THE APP app.listen(3000); More use case for cronjobs Similarly, there can be many use cases where we can automate our workflow using the corn job scheduler. Example such as: Send Scheduled Emails Maintaining Error Logs Cleaning and Maintaining Database β¦ etc. Conclusion In this article, you learned how to use node-cron to schedule jobs on a Node.js server. You were introduced to the broader concept of automating repetitive tasks in a consistent and predictable manner. This concept can be applied to your current and future projects. Get More tutorials on NodeJS Share this:TwitterFacebookRedditLinkedInWhatsAppPrintTumblr Related Tags: cron job, Express, nodejs, SQLite