Search code examples
node.jspm2

How to only restart node process with PM2 if exit code != 0


I'm using pm2 to manage a node process. Currently, pm2 restarts the node process even if it finishes cleanly (with exit code 0). I don't want that to happen.

Instead I only want PM2 to restart the app when the node process exits with a code != 0.

How to do this?

The pm2 logs might be useful:

PM2        | App [xxx] with id [0] and pid [44797], exited with code [0] via signal [SIGINT]
PM2        | Starting execution sequence in -fork mode- for app name:xxx id:0
PM2        | App name:xxx id:0 online

EDIT:

Seems that starting process in cluster-mode works as I expect. I.e.: restarts only happen on exit-codes !=0.

Still starting in fork-mode gives the unexpected behavior as described above.


Solution

  • Late to this thread, but I believe pm2 support this now. Check out https://pm2.keymetrics.io/docs/usage/restart-strategies/ and the section "Skip Auto Restart For Specific Exit Codes"

    Edit: Sorry, this is not the solution yet. There is an open issue related to this pm2 option here: https://github.com/Unitech/pm2/issues/5208. From the looks of it I don't expect it to be resolved anytime soon. So the above option should work but I don't think it does at the moment with the current version of pm2 as of this writing (5.1.12)