如果退出代码!= 0,如何仅使用 PM2 重新启动节点进程
How to only restart node process with PM2 if exit code != 0
我正在使用 pm2 来管理节点进程。
目前,pm2 重新启动节点进程,即使它干净地完成(退出代码为 0)。我不希望发生这种情况。
相反,我只希望 PM2 在节点进程以代码 != 0 退出时重新启动应用程序。
如何操作?
pm2 日志可能有用:
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
编辑:
似乎在集群模式下启动进程如我所料。即:重启仅发生在退出代码 !=0 上。
仍然以 fork 模式启动会出现如上所述的意外行为。
将 --no-autorestart
选项添加到 pm2 start
,或在您的 JSON 配置文件中。
我看过pm2
的代码
https://github.com/Unitech/pm2/blob/6090b0971abca6fcb2d796e560f2a72b81ab5707/lib/God.js
并且在成功退出时不启动进程方面似乎没有任何逻辑。您要求的功能不存在。集群模式和fork模式相同。
您可以使用 test.js
进行测试
setTimeout(()=>process.exit(), 2000);
分叉模式
$ pm2 start test.js && sleep 5
[PM2] Starting /Users/tarunlalwani/Documents/Projects/SO/pm2exit/test.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬─────────┬──────┬──────┬────────┬─────────┬────────┬─────┬──────────┬──────────────┬──────────┐
│ App name │ id │ version │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
├──────────┼────┼─────────┼──────┼──────┼────────┼─────────┼────────┼─────┼──────────┼──────────────┼──────────┤
│ test │ 0 │ N/A │ fork │ 5889 │ online │ 0 │ 0s │ 0% │ 9.4 MB │ tarunlalwani │ disabled │
└──────────┴────┴─────────┴──────┴──────┴────────┴─────────┴────────┴─────┴──────────┴──────────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
$ pm2 logs
PM2 | 2019-08-18T11:40:23: PM2 log: App [test:0] exited with code [0] via signal [SIGINT]
PM2 | 2019-08-18T11:40:23: PM2 log: App [test:0] starting in -fork mode-
PM2 | 2019-08-18T11:40:23: PM2 log: App [test:0] online
PM2 | 2019-08-18T11:40:25: PM2 log: App [test:0] exited with code [0] via signal [SIGINT]
PM2 | 2019-08-18T11:40:25: PM2 log: App [test:0] starting in -fork mode-
PM2 | 2019-08-18T11:40:25: PM2 log: App [test:0] online
$ pm2 delete test
$ pm2 start test.js -i 2&& sleep 5
[PM2] Starting /Users/tarunlalwani/Documents/Projects/SO/pm2exit/test.js in cluster_mode (2 instances)
[PM2] Done.
┌──────────┬────┬─────────┬─────────┬──────┬────────┬─────────┬────────┬─────┬───────────┬──────────────┬──────────┐
│ App name │ id │ version │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
├──────────┼────┼─────────┼─────────┼──────┼────────┼─────────┼────────┼─────┼───────────┼──────────────┼──────────┤
│ test │ 0 │ N/A │ cluster │ 5993 │ online │ 0 │ 0s │ 0% │ 27.6 MB │ tarunlalwani │ disabled │
│ test │ 1 │ N/A │ cluster │ 5994 │ online │ 0 │ 0s │ 0% │ 20.8 MB │ tarunlalwani │ disabled │
└──────────┴────┴─────────┴─────────┴──────┴────────┴─────────┴────────┴─────┴───────────┴──────────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
$ pm2 logs
PM2 | App name:test id:0 disconnected
PM2 | App [test:0] exited with code [0] via signal [SIGINT]
PM2 | App [test:0] starting in -cluster mode-
PM2 | App name:test id:1 disconnected
PM2 | App [test:1] exited with code [0] via signal [SIGINT]
PM2 | App [test:1] starting in -cluster mode-
PM2 | App [test:0] online
PM2 | App [test:1] online
PM2 | App name:test id:0 disconnected
PM2 | App [test:0] exited with code [0] via signal [SIGINT]
PM2 | App [test:0] starting in -cluster mode-
PM2 | App name:test id:1 disconnected
PM2 | App [test:1] exited with code [0] via signal [SIGINT]
PM2 | App [test:1] starting in -cluster mode-
PM2 | App [test:0] online
PM2 | App [test:1] online
PM2 | App name:test id:0 disconnected
$ pm2 delete test
备选
作为替代方法,您可以使用 Supervisord
可以使用配置文件中的exitcodes
http://supervisord.org/configuration.html
The list of “expected” exit codes for this program used with autorestart
. If the autorestart
parameter is set to unexpected, and the process exits in any other way than as a result of a supervisor stop request, supervisor
d will restart the process if it exits with an exit code that is not defined in this list.
这个帖子来晚了,但我相信 pm2 现在支持这个。查看 https://pm2.keymetrics.io/docs/usage/restart-strategies/ 和“跳过特定退出代码的自动重启”部分
编辑:抱歉,这还不是解决方案。这里有一个与此 pm2 选项相关的未决问题:https://github.com/Unitech/pm2/issues/5208。从它的外观来看,我不希望它很快得到解决。所以上面的选项 应该 工作,但我认为在撰写本文时,目前的 pm2 版本 (5.1.12)
目前还不能工作
我正在使用 pm2 来管理节点进程。 目前,pm2 重新启动节点进程,即使它干净地完成(退出代码为 0)。我不希望发生这种情况。
相反,我只希望 PM2 在节点进程以代码 != 0 退出时重新启动应用程序。
如何操作?
pm2 日志可能有用:
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
编辑:
似乎在集群模式下启动进程如我所料。即:重启仅发生在退出代码 !=0 上。
仍然以 fork 模式启动会出现如上所述的意外行为。
将 --no-autorestart
选项添加到 pm2 start
,或在您的 JSON 配置文件中。
我看过pm2
https://github.com/Unitech/pm2/blob/6090b0971abca6fcb2d796e560f2a72b81ab5707/lib/God.js
并且在成功退出时不启动进程方面似乎没有任何逻辑。您要求的功能不存在。集群模式和fork模式相同。
您可以使用 test.js
setTimeout(()=>process.exit(), 2000);
分叉模式
$ pm2 start test.js && sleep 5
[PM2] Starting /Users/tarunlalwani/Documents/Projects/SO/pm2exit/test.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬─────────┬──────┬──────┬────────┬─────────┬────────┬─────┬──────────┬──────────────┬──────────┐
│ App name │ id │ version │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
├──────────┼────┼─────────┼──────┼──────┼────────┼─────────┼────────┼─────┼──────────┼──────────────┼──────────┤
│ test │ 0 │ N/A │ fork │ 5889 │ online │ 0 │ 0s │ 0% │ 9.4 MB │ tarunlalwani │ disabled │
└──────────┴────┴─────────┴──────┴──────┴────────┴─────────┴────────┴─────┴──────────┴──────────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
$ pm2 logs
PM2 | 2019-08-18T11:40:23: PM2 log: App [test:0] exited with code [0] via signal [SIGINT]
PM2 | 2019-08-18T11:40:23: PM2 log: App [test:0] starting in -fork mode-
PM2 | 2019-08-18T11:40:23: PM2 log: App [test:0] online
PM2 | 2019-08-18T11:40:25: PM2 log: App [test:0] exited with code [0] via signal [SIGINT]
PM2 | 2019-08-18T11:40:25: PM2 log: App [test:0] starting in -fork mode-
PM2 | 2019-08-18T11:40:25: PM2 log: App [test:0] online
$ pm2 delete test
$ pm2 start test.js -i 2&& sleep 5
[PM2] Starting /Users/tarunlalwani/Documents/Projects/SO/pm2exit/test.js in cluster_mode (2 instances)
[PM2] Done.
┌──────────┬────┬─────────┬─────────┬──────┬────────┬─────────┬────────┬─────┬───────────┬──────────────┬──────────┐
│ App name │ id │ version │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
├──────────┼────┼─────────┼─────────┼──────┼────────┼─────────┼────────┼─────┼───────────┼──────────────┼──────────┤
│ test │ 0 │ N/A │ cluster │ 5993 │ online │ 0 │ 0s │ 0% │ 27.6 MB │ tarunlalwani │ disabled │
│ test │ 1 │ N/A │ cluster │ 5994 │ online │ 0 │ 0s │ 0% │ 20.8 MB │ tarunlalwani │ disabled │
└──────────┴────┴─────────┴─────────┴──────┴────────┴─────────┴────────┴─────┴───────────┴──────────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
$ pm2 logs
PM2 | App name:test id:0 disconnected
PM2 | App [test:0] exited with code [0] via signal [SIGINT]
PM2 | App [test:0] starting in -cluster mode-
PM2 | App name:test id:1 disconnected
PM2 | App [test:1] exited with code [0] via signal [SIGINT]
PM2 | App [test:1] starting in -cluster mode-
PM2 | App [test:0] online
PM2 | App [test:1] online
PM2 | App name:test id:0 disconnected
PM2 | App [test:0] exited with code [0] via signal [SIGINT]
PM2 | App [test:0] starting in -cluster mode-
PM2 | App name:test id:1 disconnected
PM2 | App [test:1] exited with code [0] via signal [SIGINT]
PM2 | App [test:1] starting in -cluster mode-
PM2 | App [test:0] online
PM2 | App [test:1] online
PM2 | App name:test id:0 disconnected
$ pm2 delete test
备选
作为替代方法,您可以使用 Supervisord
可以使用配置文件中的exitcodes
http://supervisord.org/configuration.html
The list of “expected” exit codes for this program used with
autorestart
. If theautorestart
parameter is set to unexpected, and the process exits in any other way than as a result of a supervisor stop request,supervisor
d will restart the process if it exits with an exit code that is not defined in this list.
这个帖子来晚了,但我相信 pm2 现在支持这个。查看 https://pm2.keymetrics.io/docs/usage/restart-strategies/ 和“跳过特定退出代码的自动重启”部分
编辑:抱歉,这还不是解决方案。这里有一个与此 pm2 选项相关的未决问题:https://github.com/Unitech/pm2/issues/5208。从它的外观来看,我不希望它很快得到解决。所以上面的选项 应该 工作,但我认为在撰写本文时,目前的 pm2 版本 (5.1.12)
目前还不能工作