How do I structure cron jobs in my Node.js app?
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.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Cron Jobs Summary and Checklist Complete Guide for Node.js Scheduled Tasks
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).
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.
Still have questions?
Browse all our FAQs or reach out to our support team
