Nodejs PM2 启动管理多个 app 出现 id 混乱是神马情况?
Nodejs PM2 启动管理多个 app 出现 id 混乱是神马情况?
首先我使用了不同的启动方式:
pm2 start index.js
pm2 start process.json
启动了 3 个 app ,第一次执行是正常的:
$ pm2 list
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────────────┼──────────┤
│ index │ 0 │ fork │ 48337 │ online │ 0 │ 2m │ 23.746 MB │ disabled │
│ lianj-pc │ 1 │ fork │ 48871 │ online │ 1 │ 20s │ 74.781 MB │ disabled │
│ demo │ 2 │ fork │ 48889 │ online │ 1 │ 20s │ 64.785 MB │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────────────┴──────────┘
其中 index 启动端口为 8001 ,其他两个分别为 9000 , 8000 。 ** index id=0, lianj-pc id=1, demo id=2 **
那么问题来了,当我再次单独执行 pm2 start process.json 时,启动出现了问题:
# 节选自 pm2.log
2016-07-08 16:10:24: Stopping app:lianj-pc id:1
2016-07-08 16:10:24: App [lianj-pc] with id [1] and pid [48454], exited with code [0] via signal [SIGINT]
2016-07-08 16:10:24: Starting execution sequence in -fork mode- for app name:lianj-pc id:1
2016-07-08 16:10:24: ***App name:lianj-pc id:1 online***
2016-07-08 16:10:24: Stopping app:demo id:2
2016-07-08 16:10:24: App [demo] with id [2] and pid [48456], exited with code [0] via signal [SIGINT]
2016-07-08 16:10:24: Starting execution sequence in -fork mode- for app name:demo id:2
2016-07-08 16:10:24: ***App name:demo id:2 online***
2016-07-08 16:11:04: Stopping app:lianj-pc id:1
2016-07-08 16:11:04: App [lianj-pc] with id [1] and pid [48871], exited with code [0] via signal [SIGINT]
2016-07-08 16:11:05: Starting execution sequence in -fork mode- for app name:lianj-pc id:0
2016-07-08 16:11:05: ***App name:lianj-pc id:0 online***
2016-07-08 16:11:05: Stopping app:demo id:2
2016-07-08 16:11:05: App [demo] with id [2] and pid [48889], exited with code [0] via signal [SIGINT]
2016-07-08 16:11:05: Starting execution sequence in -fork mode- for app name:demo id:0
2016-07-08 16:11:05: ***App name:demo id:0 online***
2016-07-08 16:11:05: App [lianj-pc] with id [0] and pid [49126], exited with code [255] via signal [SIGINT]
2016-07-08 16:11:05: Starting execution sequence in -fork mode- for app name:demo id:0
2016-07-08 16:11:05: App name:demo id:0 online
2016-07-08 16:11:05: App [demo] with id [0] and pid [49144], exited with code [255] via signal [SIGINT]
2016-07-08 16:11:05: Starting execution sequence in -fork mode- for app name:demo id:0
2016-07-08 16:11:05: App name:demo id:0 online
2016-07-08 16:11:05: App [demo] with id [0] and pid [49158], exited with code [255] via signal [SIGINT]
2016-07-08 16:11:05: Starting execution sequence in -fork mode- for app name:demo id:0
2016-07-08 16:11:05: App name:demo id:0 online
2016-07-08 16:11:05: App [demo] with id [0] and pid [49183], exited with code [255] via signal [SIGINT]
2016-07-08 16:11:05: Starting execution sequence in -fork mode- for app name:demo id:0
2016-07-08 16:11:05: App name:demo id:0 online
2016-07-08 16:11:05: App [demo] with id [0] and pid [49188], exited with code [255] via signal [SIGINT]
"pm2.log" 11301L, 938877C
2016-07-08 16:22:21: App [lianj-pc] with id [0] and pid [17471], exited with code [255] via signal [SIGINT]
2016-07-08 16:22:21: Starting execution sequence in -fork mode- for app name:lianj-pc id:0
请看我在日志中加了***的部分,启动 id 和 app name 的对应已经发生混乱,都对应到了 index 的 id 上去,结果就是 index 不断重启,但是又因为重复绑定端口而报错,日志如下:
M2 App [lianj-pc] with id [0] and pid [66856], exited with code [255] via signal [SIGINT]
PM2 Starting execution sequence in -fork mode- for app name:lianj-pc id:0
PM2 App name:lianj-pc id:0 online
lianj-pc-0 Error: listen EADDRINUSE 127.0.0.1:8001
lianj-pc-0 at Object.exports._errnoException (util.js:870:11)
lianj-pc-0 at exports._exceptionWithHostPort (util.js:893:20)
lianj-pc-0 at Server._listen2 (net.js:1234:14)
lianj-pc-0 at listen (net.js:1270:10)
lianj-pc-0 at net.js:1379:9
lianj-pc-0 at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:64:16)
lianj-pc-0 at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:83:10)
想从 Google 问个究竟,却始终查不出什么,不知道大家有没有遇到的,目前来看会不会就是不能混合使用启动方式呢?
针对Node.js中PM2启动管理多个app时出现id混乱的问题,这通常不是PM2本身的bug,而是可能由于配置不当或操作错误导致的。以下是一些可能的解决步骤和建议:
-
检查配置文件: 如果你使用了PM2的配置文件(如ecosystem.config.js),请确保每个app的配置都是正确的,并且每个app都有一个唯一的名称。例如:
module.exports = { apps: [ { name: 'app1', script: 'app1.js' }, { name: 'app2', script: 'app2.js' } ] };
-
使用正确的命令: 启动、停止、重启等操作时,确保指定了正确的app名称或id。例如,使用
pm2 start ecosystem.config.js --only app1
来启动特定的app。 -
查看PM2列表: 使用
pm2 list
查看当前所有运行的app及其对应的id和名称,这有助于你确认是否有重复或错误的id。 -
更新PM2: 确保你使用的PM2是最新版本,可以通过
pm2 update pm2
进行更新。 -
查看日志: 使用
pm2 logs
查看详细的日志输出,这有助于诊断问题。
如果以上步骤都无法解决问题,建议查看PM2的官方文档或在社区论坛中寻求帮助。