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
?
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
.
Once started, your app is forever alive, auto-restarting across crashes and machine restarts
. –
Oneirocritic max_restarts
or autorestart: false
in your configuration. Try asking on the pm2 issue tracker. –
Oneirocritic min_uptime
(1s by default) until max_restarts
(15 by default) is exceeded, it will stop restarting. So the most likely explanation here is that your app keeps crashing over and over for some reason. Whenever it stays up for longer than min_uptime
, max_restarts
is reset, so the default values should work just fine. –
Oneirocritic 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.
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.
This may help:
# Generate Startup Script
$ pm2 startup
# Freeze your process list across server restart
$ pm2 save
# Remove Startup Script
$ pm2 unstartup
More details here
© 2022 - 2024 — McMap. All rights reserved.