Should I set autorestart: false for PM2 cron jobs?
Yes. Cron jobs are one-time scripts that run and exit. Without autorestart: false, PM2 would restart the script immediately after it exits, causing it to run continuously instead of only at the scheduled cron time.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Using PM2 cron-start for Recurring Tasks Server-Level Scheduling
Use pm2 start script.js --cron '0 0 * * *' --name task-name. Or define in ecosystem.config.js with cron: '0 0 * * *' and autorestart: false. PM2 runs the script at the scheduled time, captures logs, and manages restarts.
Use PM2 cron for independent tasks (backups, log rotation, system checks) that don't need app context. Use node-cron for tasks integrated with your app (using models, cache, database connections) that should stop when the app stops.
Create a standalone script that connects to the database, performs the task, and exits with process.exit(0) on success or process.exit(1) on failure. Load dotenv, connect to MongoDB, run the task, disconnect, and exit. PM2 will run it at the scheduled cron time.
Still have questions?
Browse all our FAQs or reach out to our support team
