Reputation: 21877
I have a Node.js app ready which is workable, but has known and unknown bugs which crash the app. In such cases it would be nice if pm2
can restart the node app. Is this feature already available in pm2
?
Upvotes: 46
Views: 92676
Reputation: 76209
Yes, it does this by default. For more information see Restart strategies.
If the app repeatedly fails to start over a short period of time, pm2 may cease restarting. See configuration, min_uptime
and max_restarts
.
Upvotes: 50
Reputation: 4323
To make app restart when it crashes you have to use one of PM2's restart strategies.
There is something called "Exponential Backoff Restart Delay" which PM2 explains as:
Instead of restarting your application like crazy when exceptions happens (e.g. database is down), the exponential backoff restart will increase incrementaly the time between restarts.
You can set it using the CLI like this:
pm2 start app.js --exp-backoff-restart-delay=100
There are other restart methods also, which are mentioned here.
Upvotes: 12
Reputation: 1134
Also, check this new excellent option:
--exp-backoff-restart-delay=100
pm2
will restart the crashed app after 100 milliseconds (0.1 seconds), then step-by-step increase restart-delay to 15 seconds.
Upvotes: 12