Cron Jobs: These are the tasks that run periodically by the operating system. Users can schedule commands the OS will run these commands automatically according to the given time. It is usually used for system admin jobs such as backups, logging, sending newsletters, subscription emails and more.
Prerequisites:
- Node.js installed
- NPM installed
- Basic knowledge of Node.js syntax
We will use a package called node-cron which is a task scheduler in pure JavaScript for node.js. We are also using express as a server. Install the required packages using the command
npm install express node-cron
Syntax:
cron.schedule("* * * * *", function() {
// Task to do
});
Time formatting for Cron jobs:

Descriptors with their ranges:
- Seconds (optional): 0 – 59
- Minute: 0 – 59
- Hour: 0 – 23
- Day of the Month: 1 – 31
- Month: 1 – 12
- Day of the week: 0 – 7 (0 and 7 both represent Sunday)
Examples:
- (*/10 * * * *) – Runs on every 10 minutes
- (* * 21 * *) – Runs 21th of every month
- (0 8 * * 1) – Runs 8 AM on every Monday
Example: Create a new file named index.js and add the following code:
const cron = require( "node-cron" );
const express = require( "express" );
app = express();
cron.schedule( "*/10 * * * * *" , function () {
console.log( "running a task every 10 second" );
});
app.listen(3000);
|
Run the file using command node index
, you will see the output like below:

Writing to a log file: Cron jobs can be used to schedule logging tasks in a system. We can log server status for a given time for monitoring purposes.
Example:
const cron = require( "node-cron" );
const express = require( "express" );
const fs = require( "fs" );
app = express();
cron.schedule( "*/10 * * * * *" , function () {
let data = `${ new Date().toUTCString()}
: Server is working\n`;
fs.appendFile( "logs.txt" , data, function (err) {
if (err) throw err;
console.log( "Status Logged!" );
});
});
app.listen(3000);
|
After running above file for 30-40 seconds, you will see a file created named logs.txt
having content somewhat similar to the following:

Monthly Newsletters: Sending monthly newsletters are also a use case for cron jobs in which an email will be sent to the users monthly with the latest products or blog information on the website.
You can learn more about sending email in Node.js here.
Example:
const cron = require( "node-cron" );
const express = require( "express" );
const nodemailer = require( "nodemailer" );
app = express();
cron.schedule( "*/1 * * * *" , function () {
sendMail();
});
function sendMail() {
let mailTransporter = nodemailer.createTransport({
service: "gmail" ,
auth: {
user: "<your-email>@gmail.com" ,
pass: "**********"
}
});
let mailDetails = {
from: "<your-email>@gmail.com" ,
to: "<user-email>@gmail.com" ,
subject: "Test mail using Cron job" ,
text: "Node.js cron job email"
+ " testing for GeeksforGeeks"
};
mailTransporter.sendMail(mailDetails,
function (err, data) {
if (err) {
console.log( "Error Occurs" , err);
} else {
console.log( "Email sent successfully" );
}
});
}
app.listen(3000);
|
The above script will send emails every minute.
Note: Open this link to Allow less secure apps: ON.
Now run the file using node index.js
, you will see the output like below:
In console:

In Gmail Inbox:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!