How to Keep Node.js Cron Jobs Running After Server Crash

💡 How to Keep Your Node.js Cron Jobs Alive — Even When the Server Crashes Ever had your Node.js server crash and suddenly your cron jobs stopped running? It’s one of those “oh no” moments we all hit eventually. 😅 Here’s what really happens: Most of us set up cron jobs inside the Node app (using node-cron, agenda, etc.). But when the app crashes or restarts, those scheduled tasks die with it. So how do we fix it? 🚀 3 battle-tested approaches: 1️⃣ Use a Process Manager (PM2 / Docker) Let PM2 auto-restart your Node process if it crashes. Simple setup Great for small deployments Still pauses jobs during restarts 2️⃣ Run Cron Jobs Externally (Best Practice) Move cron logic outside your main server. Use a separate worker process (worker.js) Or a Linux cron / Kubernetes CronJob / AWS CloudWatch Event Keeps jobs running even if your web app crashes 3️⃣ Use a Managed Scheduler If you’re in the cloud: AWS Lambda + CloudWatch Events Google Cloud Scheduler Temporal.io, BullMQ, or Agenda with Redis These run independently and reliably. 💡 Bonus tip: If multiple app instances are running, use a distributed lock (e.g. Redlock + Redis) so only one instance executes the job at a time. ✅ My Recommendation: Keep your cron logic separate from the main API. Use PM2 (or Docker) to keep both the API and worker alive. Add a Redis lock if scaling horizontally. This setup has saved me (and my teams) from so many “why didn’t the job run?” mysteries. 😎 How are you managing your cron jobs in production? Curious to hear other setups. 👇 #NodeJS #Backend #DevOps #PM2 #CronJobs #SoftwareEngineering #WebDevelopment

To view or add a comment, sign in

Explore content categories