pm2
max-restarts
continuous restarting
crash
troubleshooting

pm2 --max-restarts limit not working and continuous restarting crash host system

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

PM2 `--max-restarts` Limit Not Working and Continuous Restarting Crashes Host System

PM2 is a popular process manager for Node.js applications that assists in managing application uptime and handling crashes. One of its features is the ability to configure options like `--max-restarts`, which limits the number of times PM2 will attempt to restart a process that has crashed. However, situations may arise where this limit appears to be ineffective, resulting in a continuous restart loop that could potentially crash the host system. In this article, we delve into the possible causes and technical explanations for this issue, offering insights into preventive measures and troubleshooting.

Understanding PM2 and the `--max-restarts` Option

PM2 is designed to enhance the stability and resilience of Node.js applications. By setting configurations such as memory limits, execution constraints, and restart parameters, PM2 ensures applications remain active and recover from unexpected failures. The `--max-restarts` flag is a critical setting specifically intended to limit the number of automatic restarts to prevent excessive resource consumption.

  • Exit Code 0: Indicates a normal exit. PM2 recognizes this and should not attempt a restart.
  • Exit Code >0: Denotes an error or abnormal exit, prompting PM2 to trigger a restart.
  • Excessive CPU Load: Increased processor cycles from frequent start attempts.
  • Memory Drain: Continual memory allocation for processes.
  • Network Problems: High restart frequency causing network congestion.
  • Review Setup and Logs: Audit `ecosystem.config.js` and other relevant configuration files for any discrepancies. Analyze PM2 logs (`pm2 logs app`) to identify potential causes for continuous restarts.
  • Implement Notification Mechanism: Configure notifications for critical thresholds using PM2's built-in event hooks (`pm2 on sigusr2`). This strategy offers real-time alerts, allowing for prompt intervention.
  • Use Additional Flags: Explore other PM2 flags for more fine-grained control:
    • `--autorestart`: Enables/disables automatic restarts (`true` by default).
    • `--restart-delay`: Time (in milliseconds) to wait between restarts.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.