Facebook Pixel

Cron Jobs Summary and Checklist Complete Guide for Node.js Scheduled Tasks

A comprehensive summary and checklist for implementing cron jobs in your Node.js application from setup to production best practices.

Cron Jobs Summary and Checklist

This is a complete summary of everything you need to know about cron jobs in Node.js.

Cron Job Setup Checklist

  • Install node-cron (npm install node-cron)
  • Create a cron jobs module (src/cron/index.js)
  • Schedule all recurring tasks with cron expressions
  • Wrap each job in try-catch
  • Log job start, completion, and errors
  • Track job execution in a database
  • Send alerts on failure
  • Set timeouts for each job
  • Make jobs idempotent
  • Handle timezones
  • Prevent overlapping runs
  • Test each job independently
  • Document all jobs in a registry

Cron Expression Cheat Sheet

* * * * *  - Every minute
*/5 * * * *  - Every 5 minutes
*/15 * * * *  - Every 15 minutes
*/30 * * * *  - Every 30 minutes
0 * * * *  - Every hour
0 */6 * * *  - Every 6 hours
0 0 * * *  - Every day at midnight
0 9 * * *  - Every day at 9 AM
0 0 * * 0  - Every Sunday at midnight
0 0 * * 1  - Every Monday at midnight
0 9 * * 1-5  - Weekdays at 9 AM
0 0 1 * *  - First of every month at midnight
0 0 1 1 *  - January 1st at midnight

Architecture Summary

node-cron (in-app)
├── Data cleanup (daily at midnight)
├── Digest emails (daily at 9 AM)
├── Weekly reports (Monday at midnight)
├── Health checks (every 5 minutes)
└── Cache warming (daily at 5 AM)

PM2 cron (separate scripts)
├── Database backup (daily at 2 AM)
├── Log rotation (weekly on Sunday)
└── System health check (every 15 minutes)

Code Template

// src/cron/index.js const cron = require("node-cron"); const { runCronJob } = require("./utils"); const { cleanup, sendDigest, generateReport, healthCheck } = require("./jobs"); // Daily cleanup at midnight UTC cron.schedule("0 0 * * *", () => { runCronJob("daily-cleanup", cleanup); }, { timezone: "UTC" }); // Daily digest at 9 AM IST cron.schedule("0 9 * * *", () => { runCronJob("daily-digest", sendDigest); }, { timezone: "Asia/Kolkata" }); // Weekly report on Monday at midnight cron.schedule("0 0 * * 1", () => { runCronJob("weekly-report", generateReport); }, { timezone: "UTC" }); // Health check every 5 minutes cron.schedule("*/5 * * * *", () => { runCronJob("health-check", healthCheck); }); console.log("Cron jobs scheduled");
// src/cron/utils.js const isRunning = {}; const runCronJob = async (jobName, jobFn) => { if (isRunning[jobName]) { console.log(`[${jobName}] Already running, skipping`); return; } isRunning[jobName] = true; const startTime = Date.now(); try { console.log(`[${jobName}] Started`); const result = await jobFn(); const duration = Date.now() - startTime; console.log(`[${jobName}] Completed in ${duration}ms`); await CronJob.create({ jobName, status: "completed", startTime: new Date(startTime), duration, result }); } catch (err) { const duration = Date.now() - startTime; console.error(`[${jobName}] Failed after ${duration}ms:`, err.message); await CronJob.create({ jobName, status: "failed", startTime: new Date(startTime), duration, error: err.message, errorStack: err.stack }); await sendAlert(`[${jobName}] Failed: ${err.message}`); } finally { isRunning[jobName] = false; } }; module.exports = { runCronJob };

The Takeaway

Cron jobs are essential for recurring tasks in Node.js. Use node-cron for app-integrated tasks (cleanup, notifications, reports) and PM2 cron for independent server tasks (backups). Always wrap in try-catch, log everything, track execution in a database, send alerts on failure, make jobs idempotent, set timeouts, handle timezones, prevent overlapping runs, and document all jobs. Test each job independently before deploying.

Install node-cron, create a cron module, schedule tasks with cron expressions, wrap in try-catch, log start/completion/errors, track in a database, send alerts on failure, set timeouts, make idempotent, handle timezones, prevent overlapping, and document in a registry.

0 0 * * * (daily midnight), 0 9 * * * (daily 9 AM), */15 * * * * (every 15 min), 0 * * * * (hourly), 0 0 * * 0 (Sundays), 0 9 * * 1-5 (weekdays 9 AM), 0 0 1 * * (monthly 1st), 0 */6 * * * (every 6 hours).

Create src/cron/index.js that schedules all jobs, src/cron/jobs/ with individual job functions, and src/cron/utils.js with the runCronJob wrapper (handles try-catch, logging, tracking, alerts, overlap prevention). Load the cron module in your server.js entry file.

Track jobName, status (running/completed/failed), startTime, endTime, duration, result (for successful jobs), error and errorStack (for failed jobs). Store in a CronJob MongoDB collection. Create a /health/cron endpoint to view job history.

Make jobs idempotent (prevent duplicates), handle timezones, set timeouts, use distributed locks for multiple instances, gradual rollout (10% first), monitor with CloudWatch metrics, alert on failures, and document all jobs with name, schedule, description, timeout, and retries in a registry.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.