Talespin_Kit
Talespin_Kit

Reputation: 21877

Can the pm2 node module restarts the app after crash automatically

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

Answers (4)

jgillich
jgillich

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

illiteratewriter
illiteratewriter

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

xoid
xoid

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

Alliswell
Alliswell

Reputation: 1583

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

Upvotes: 5

Related Questions