How do I monitor cron jobs in production?
Track job execution in a database (CronJob collection with status, duration, result, error). Send metrics to CloudWatch (success/failure count per job). Set up alerts (email, Slack) for failures. Create a /health/cron endpoint to view job status. Document all jobs in a registry with name, schedule, timeout, and retries.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Cron Jobs in Production Best Practices for Reliable Scheduled Tasks
Track job execution in a database. Before running, check if the job already ran for this period (e.g., DigestLog.findOne({ date: today })). If it exists, skip. After running, create a record. This prevents duplicate emails or data processing if the job runs twice.
Specify timezone in cron.schedule options: cron.schedule('0 9 * * *', fn, { timezone: 'Asia/Kolkata' }). For per-user timezones, run hourly and check which users' local time is 9 AM. This ensures users receive notifications at their local time, not the server's time.
Use a distributed lock with Redis: redis.set('lockKey', 'locked', 'NX', 'EX', 300). If another instance already acquired the lock, skip the job. Release the lock with redis.del('lockKey') after completion. The TTL ensures the lock is released if the instance crashes.
Still have questions?
Browse all our FAQs or reach out to our support team
