Many routine tasks need to happen on time, whether it’s sending updates, organizing files, or cleaning up old data. When managed manually, these steps are prone to errors or delays. Automation ensures they are carried out consistently and with minimal effort. In software systems, one of the ways to automate these routine tasks is through a cron job.

A cron job is a powerful scheduling tool that automates consistent and repetitive tasks or processes in systems. You can think of it as your digital assistant that never forgets and never sleeps.
Many of us have used “schedule payment” features in our banking or payment applications, this is an example of cron job technology at work. When you set up automatic bill payments or recurring transfers, you are leveraging the same principles that power cron jobs.
Cron jobs eliminate the need for manual intervention in repetitive processes. They ensure that important tasks get executed precisely when needed without the risk of human error or forgetfulness. This automation allows systems to run efficiently while freeing up valuable time and mental bandwidth for more strategic work.
This article introduces the concept of cron jobs using Node.js and the node-cron library.
Prerequisites
Npm
Node.js
Implementation Steps
Step 1: Initialise a new project
npm init -y
Step 2: Install the node-cron npm package
npm i node-cron
Step 3: Create an index.js file in your root directory and import node-cron
import cron from 'node-cron'
const job = cron.schedule('* * * * *', () => {
console.log('Cron job now triggered');
});
Running node index.js should result in the log above appearing in the console every minutes.
Format of a cron schedule string
The cron schedule is made up of 6 time fields, but the second field is optional and is hardly used.
# ┌────────────── second (optional)
# │ ┌──────────── minute
# │ │ ┌────────── hour
# │ │ │ ┌──────── day of month
# │ │ │ │ ┌────── month
# │ │ │ │ │ ┌──── day of week
# │ │ │ │ │ │
# │ │ │ │ │ │
# * * * * * *
Create a function to perform any scheduled calculation or task
function sum(num1, num2) {
const total = num1 + num2
console.log (total)
}
Examples
- Runs every minute between 3:00 PM and 3:59 PM every day and triggers the sumNumber function:
const job = cron.schedule('* 15 * * *', () => {
console.log('cron job now triggered');
sum(4, 8)
});
job.start()
2. Runs every minute and triggers the sumNumber function:
const job = cron.schedule('* * * * *', () => {
console.log('cron job now triggered');
sum(4, 8)
});
job.start()
3. Runs every 10 minutes from 9 AM to 5 PM, Monday to Friday and triggers the sumNumber function:
const job = cron.schedule('*/10 9-17 * * 1-5', () => {
console.log('cron job now triggered');
sum(4, 8)
});
job.start()
4. Runs by 12:30pm everyday and triggers the sumNumber function:
const job = cron.schedule('30 12 * * *', () => {
console.log('cron job now triggered');
sum(4, 8)
});
job.start()
Pros of setting up cron job with node-cron
- Very easy to set up.
- Makes use of familiar cron syntax.
- Runs within your Node.js application.
- Easy to test and debug within your development environment.
Cons of setting up cron job with node-cron
- Job runs only when your application is running so if your application fails, the job stops.
- Keeping the process running continuously to handle scheduled tasks consumes memory and system resources.
- Missed or failed jobs will not retry unless you implement retries yourself.
Conclusion
The node-cron scheduler is an excellent choice for simple, lightweight task scheduling directly within your Node.js applications. It leverages familiar cron syntax and integrates seamlessly with your codebase, making it easy to implement, test, and maintain. However, for critical or high availability tasks, considering that node-cron depends on your app’s uptime and lacks built in retry mechanisms. For such cases, dedicated job schedulers or external cron services may be more appropriate.
Thank you for reading !!!
