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
?

- 20,830
- 29
- 89
- 135
4 Answers
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
.

- 71,459
- 6
- 57
- 85
-
watch option also restarts app when files changed. how to prevent this case ? – Gor Feb 21 '18 at 10:00
-
7@Gor You don't set the watch option. The restart after crash happens without any options, sorry if that wasn't clear enough. – jgillich Feb 22 '18 at 11:44
-
12it doesnt do this in my experience… I've got a service I've got to restart once a month because it keeps crashing… – AlexK Apr 20 '18 at 09:34
-
3I 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? – 7hibault Jul 19 '18 at 08:20
-
1@7hibault It's right there on the [front page](https://pm2.io/doc/en/runtime/overview/): `Once started, your app is forever alive, auto-restarting across crashes and machine restarts`. – jgillich Jul 19 '18 at 11:49
-
2@jgillich What could cause this to not happen then? – 7hibault Jul 20 '18 at 08:46
-
@7hibault Not sure, maybe you set `max_restarts` or `autorestart: false` in your configuration. Try asking on the pm2 issue tracker. – jgillich Jul 20 '18 at 12:18
-
3@7hibault 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. – jgillich Jul 20 '18 at 12:27
-
@jgillich this was incredibly informative - thank you! All the props! – Eric Holmes Aug 16 '18 at 13:34
-
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. – xoid May 07 '19 at 12:27
-
@jgillich , due to your intense knowledge, you may enjoy this question! https://stackoverflow.com/questions/65671109/know-in-logs-or-add-explicit-text-when-a-crash-restart-happens-with-pm2 mfg – Fattie Jan 11 '21 at 16:55
-
1I'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. – Raymond Feb 05 '22 at 17:06
-
1@jgillich "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? – Raymond Feb 05 '22 at 17:08
-
@Raymond Because it's correct. :) But it was a bit brief so I've updated it with more information. – jgillich Feb 11 '22 at 14:39
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.

- 1,114
- 1
- 10
- 24
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.

- 4,155
- 1
- 23
- 44