We use cookies to enhance your experience. By continuing to visit this site you agree to our use of cookies. Learn More
contact@techquestworld.com
+919547614783
Premium Article
Published 4 months ago

Build Your Own Cron Job Manager in Node.js | Beginner-Friendly Guide

5 min read
112 views
Build Your Own Cron Job Manager in Node.js | Beginner-Friendly Guide
Photo by Techquestworld

Learn how to create your own custom Cron Job Manager in Node.js using just one simple method. Automated backups to sending scheduled emails, this guide shows you how to manage multiple recurring tasks efficiently with the powerful node-cron library.

๐Ÿ”น Project Seup

mkdir cron-job-manager
cd cron-job-manager
npm init -y

๐Ÿ”น Required Package Install

npm install node-cron

๐Ÿ”น New file

touch cronManager.js

๐Ÿ”น Souce Code

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

// Custom function to schedule cron jobs
function jobManager(timePattern, taskName, taskFunction) {
// Check if the provided cron pattern is valid
if (!cron.validate(timePattern)) {
console.error(`Invalid cron time pattern for ${taskName}`);
return;
}

// If the pattern is valid, schedule the task
cron.schedule(timePattern, () => {
console.log(
`Task "${taskName}" is running at: ${new Date().toLocaleString()}`
);
taskFunction(); // Run the actual task
});

// Success message after scheduling
console.log(`Task "${taskName}" scheduled with pattern: ${timePattern}`);
}

// Task runs every 5 seconds
jobManager("*/5 * * * * *", "My Task One", () => {
console.log("Task one is running...");
});

// Task runs at the 10th second of every minute for backup
jobManager("10 * * * * *", "Database Backup", () => {
console.log("Backup task started...");
});

๐Ÿ”น Cron Pattern Quick Chart



Pattern Use Case
* * * * * * Every second
*/5 * * * * * Every 5 seconds
10 * * * * * At 10th second of every minute
0 0 * * * Every day at midnight

๐Ÿ”น Run Your Cron Manager

node cronManager.js

Terminal Output Example:

Task "My Task One" has been scheduled with pattern: */5 * * * * *
Task "Database Backup" has been scheduled with pattern: 10 * * * * *
Task "My Task One" is running at 11:00:05
Task one is running...
Task "Database Backup" is running at 11:01:10
Backup task started...

You've now built your first custom Cron Job Manager in Node.js.

Scheduling multiple tasks using just one method has become super easy.

You can use this for:

* Daily report generation

* Auto-hitting APIs

* Email reminders

* File cleaner scheduler


๐Ÿ’ฌ What idea do you have for this project? Share it in the comments below.

๐Ÿ”น For more such tutorials, follow our blog โ€“ TechQuestWorld.

๐Ÿ“บ Subscribe to our YouTube Channel for practical dev content:

๐Ÿ‘‰ @theroxycoder

Author
TAPAS SAHOO

Developer by Profession, Techie by Heart

A curious mind with a love for writing and technology, dedicated to simplifying web development and programming topics while keeping up with the ever-changing tech landscape.

Discussion (0)

Replying to
Previous Article
Build Your Own Code Snippet Manager with Reac...

Make your own React-based Code Snippet Manager app โ€” save, tag and search reusable code blocks easil...

Next Article
Why Node.js Is Still the Best Backend Choice...

Still wondering if Node.js is the right backend tech in 2025? This article breaks down exactly why N...

Related Articles

Laravel vs NodeJS in 2025: Which One Should You Learn First?
Laravel vs NodeJS in 2025: Which One Should You Le...

Laravel or NodeJS โ€“ which one should you learn in 2025? This guide helps you choose the right backen...

Node.js Async vs Await Explained with Real Examples
Node.js Async vs Await Explained with Real Example...

Master async and await in Node.js with this easy guide. Includes real code examples, pros/cons and p...

ExpressJS vs NestJS: Which Node.js Framework Should You Choose in 2025?
ExpressJS vs NestJS: Which Node.js Framework Shoul...

Discover the core differences between ExpressJS and NestJS. Whether you build APIs or full-scale app...

Why Node.js Is Still the Best Backend Choice in 2025 โ€“ Performance, Scalability & Beyond
Why Node.js Is Still the Best Backend Choice in 20...

Still wondering if Node.js is the right backend tech in 2025? This article breaks down exactly why N...