如果退出代码!= 0,如何仅使用PM2重新启动节点进程

时间:2017-04-25 14:48:46

标签: node.js pm2

我正在使用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模式启动会产生如上所述的意外行为。

2 个答案:

答案 0 :(得分:1)

我看过pm2

的代码

https://github.com/Unitech/pm2/blob/6090b0971abca6fcb2d796e560f2a72b81ab5707/lib/God.js

在成功退出时不启动进程方面似乎没有任何逻辑。您要的功能不存在。集群和分叉模式都一样。

您可以使用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

  

autorestart一起使用的该程序的“预期”退出代码列表。如果将autorestart参数设置为意外情况,并且该进程以除主管停止请求之外的任何其他方式退出,则supervisor d将以以下退出代码退出以重启该进程:在此列表中未定义。

答案 1 :(得分:0)

--no-autorestart选项添加到pm2 start或JSON配置文件中。