Can the pm2 node module restarts the app after crash automatically
Asked Answered
L

4

45

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?

Luetic answered 1/10, 2014 at 6:29 Comment(0)
O
50

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.

Oneirocritic answered 1/10, 2014 at 6:57 Comment(14)
watch option also restarts app when files changed. how to prevent this case ?Goldshell
@Goldshell You don't set the watch option. The restart after crash happens without any options, sorry if that wasn't clear enough.Oneirocritic
it doesnt do this in my experience… I've got a service I've got to restart once a month because it keeps crashing…Thier
I got a crash of a node process started with pm2 that didn't restart. Maybe I've configured something wrong. I can't find doc related to this default restart after crash, could you point out where it is, please?Damnable
@Damnable It's right there on the front page: Once started, your app is forever alive, auto-restarting across crashes and machine restarts.Oneirocritic
@Oneirocritic What could cause this to not happen then?Damnable
@Damnable Not sure, maybe you set max_restarts or autorestart: false in your configuration. Try asking on the pm2 issue tracker.Oneirocritic
@Damnable When your app stays up for less than 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
@Oneirocritic this was incredibly informative - thank you! All the props!Paver
suppose my app crashed because db is down. After max_restart pm2 will not restart the app, even if db is up. It is bad. Would be better to increase restart time to 1 minute but still trying to restart.Maisonette
@Oneirocritic , due to your intense knowledge, you may enjoy this question! #65671609 mfgAbhorrent
I've few node.js based and it doesn't restart after crash (or whatever reason it gets killed for). It's just freaking me out and it's pain in the a$$. I've started to hate node.js for this reason.Westlund
@Oneirocritic "Yes, it does this by default and there is even a watch option to restart on changes." This statement is not correct. My server gets killed plenty of time so it is certainly not by default restarted. Unless you do something to it, it will not work. Why is this accepted answer?Westlund
@Westlund Because it's correct. :) But it was a bit brief so I've updated it with more information.Oneirocritic
M
12

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.

Maisonette answered 7/5, 2019 at 12:55 Comment(0)
I
12

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.

Insignia answered 24/3, 2020 at 10:14 Comment(0)
C
5

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

Conatus answered 9/10, 2019 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.