node-cron for scheduled jobs

## Installing and Using Cron in Node.js: Create Recurring Tasks and Automate Processes

Automating tasks is crucial for any application. In Node.js, you can easily schedule the execution of functions and scripts using libraries like node-cron. This …


This content originally appeared on DEV Community and was authored by Lucas Pereira de Souza

logotech

## Installing and Using Cron in Node.js: Create Recurring Tasks and Automate Processes

Automating tasks is crucial for any application. In Node.js, you can easily schedule the execution of functions and scripts using libraries like node-cron. This article explores how to install, configure, and use node-cron to create recurring tasks, with a practical example of reminders.

Installing node-cron

Installing node-cron is simple. Use the npm (Node Package Manager) package manager in your terminal:

npm install node-cron

Creating Recurring Tasks

With node-cron installed, you can start scheduling your tasks. The library uses a cron-based syntax to define how often tasks should be executed. The basic syntax is:

* * * * * *
│ │ │ │ │ │
│ │ │ │ │ └─ Day of the week (0 - 7, where 0 and 7 represent Sunday)
│ │ │ │ └─ Month (1 - 12)
│ │ │ └─ Day of the month (1 - 31)
│ │ └─ Hour (0 - 23)
│ └─ Minute (0 - 59)
└─ Second (0 - 59)

Examples:

  • '0 0 * * *': Executes the task at midnight (00:00) every day.
  • '0 12 * * 1-5': Executes the task at noon (12:00) from Monday to Friday.
  • '*/10 * * * *': Executes the task every 10 minutes.

Implementation:

const cron = require('node-cron');

// Function to be executed
const task = () => {
  console.log('Task running at ' + new Date().toLocaleString());
};

// Scheduling the task to run every minute
cron.schedule('* * * * *', task);

console.log('Task scheduled!');

Example: Reminder System

Let's create a practical example of a reminder system that sends notifications.

const cron = require('node-cron');

// Function to send the reminder (simulation)
const sendReminder = (message) => {
  console.log(`Reminder: ${message} - ${new Date().toLocaleString()}`);
  // Here you can add logic to send notifications (e-mail, SMS, etc.)
};

// Scheduling a reminder
cron.schedule('0 9 * * *', () => { // Runs every day at 9 am
  sendReminder('Reminder: Important meeting in 30 minutes!');
});

cron.schedule('30 14 * * 1-5', () => { // Runs from Monday to Friday at 2:30 pm
  sendReminder('Reminder: Time for the weekly report!');
});

console.log('Reminder system started!');

In this example:

  1. We import node-cron.
  2. We create the sendReminder function, which simulates sending a reminder. In the real world, you would use this function to send emails, push notifications, etc.
  3. We schedule two reminders: one for an important meeting and another for a weekly report.

Final Considerations

  • Error Handling: Implement error handling within your scheduled tasks to ensure that failures do not interrupt the scheduling.
  • Logs: Use logs to monitor the execution of your tasks and troubleshoot problems.
  • Persistence: For tasks that need to be executed even when the application is offline, consider using external task scheduling tools (such as an operating system cron job).
  • Testing: Test your scheduled tasks to ensure they are working as expected.

With node-cron, you have a powerful tool to automate tasks in your Node.js applications. Start using and optimizing your processes today!


This content originally appeared on DEV Community and was authored by Lucas Pereira de Souza


Print Share Comment Cite Upload Translate Updates
APA

Lucas Pereira de Souza | Sciencx (2025-10-17T16:15:46+00:00) node-cron for scheduled jobs. Retrieved from https://www.scien.cx/2025/10/17/node-cron-for-scheduled-jobs/

MLA
" » node-cron for scheduled jobs." Lucas Pereira de Souza | Sciencx - Friday October 17, 2025, https://www.scien.cx/2025/10/17/node-cron-for-scheduled-jobs/
HARVARD
Lucas Pereira de Souza | Sciencx Friday October 17, 2025 » node-cron for scheduled jobs., viewed ,<https://www.scien.cx/2025/10/17/node-cron-for-scheduled-jobs/>
VANCOUVER
Lucas Pereira de Souza | Sciencx - » node-cron for scheduled jobs. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/17/node-cron-for-scheduled-jobs/
CHICAGO
" » node-cron for scheduled jobs." Lucas Pereira de Souza | Sciencx - Accessed . https://www.scien.cx/2025/10/17/node-cron-for-scheduled-jobs/
IEEE
" » node-cron for scheduled jobs." Lucas Pereira de Souza | Sciencx [Online]. Available: https://www.scien.cx/2025/10/17/node-cron-for-scheduled-jobs/. [Accessed: ]
rf:citation
» node-cron for scheduled jobs | Lucas Pereira de Souza | Sciencx | https://www.scien.cx/2025/10/17/node-cron-for-scheduled-jobs/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.