【问题标题】:How to stop a pm2 process from systemctl如何从 systemctl 停止 pm2 进程
【发布时间】:2020-08-07 13:53:37
【问题描述】:

这是我的服务文件

[Unit]
Description=HTTP Hello World
After=network.target
[Service]
User=mukund
Environment=MY_HOST=localhost
WorkingDirectory=/home/mukund/Work/nodejs/nodeExpTutorial
ExecStart=/usr/bin/pm2 start config.json
[Install]
WantedBy=multi-user.target

config.json 在哪里

{"apps" : [
    {

      "script"     : "app.js",
      "instances"  : 3,
      "exec_mode"  : "cluster"
    }]
} 

当我运行这个命令时,我得到了这个错误

mukund@mukund-ThinkPad-E15:~/Work/nodejs/nodeExpTutorial$ systemctl status demo-node-app.service
● demo-node-app.service - HTTP Hello World
     Loaded: loaded (/etc/systemd/system/demo-node-app.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Fri 2020-08-07 19:09:00 IST; 13s ago
    Process: 38182 ExecStart=/usr/lib/node_modules/pm2/bin/pm2 config.json (code=exited, status=1/FAILURE)
   Main PID: 38182 (code=exited, status=1/FAILURE)

Aug 07 19:08:59 mukund-ThinkPad-E15 systemd[1]: Started HTTP Hello World.
Aug 07 19:09:00 mukund-ThinkPad-E15 pm2[38182]: [PM2][ERROR] Command not found
Aug 07 19:09:00 mukund-ThinkPad-E15 pm2[38182]: usage: pm2 [options] <command>
Aug 07 19:09:00 mukund-ThinkPad-E15 pm2[38182]: pm2 -h, --help             all available commands and options
Aug 07 19:09:00 mukund-ThinkPad-E15 pm2[38182]: pm2 examples               display pm2 usage examples
Aug 07 19:09:00 mukund-ThinkPad-E15 pm2[38182]: pm2 <command> -h           help on a specific command
Aug 07 19:09:00 mukund-ThinkPad-E15 pm2[38182]: Access pm2 files in ~/.pm2
Aug 07 19:09:00 mukund-ThinkPad-E15 systemd[1]: demo-node-app.service: Main process exited, code=exited, status=1/FAILURE
Aug 07 19:09:00 mukund-ThinkPad-E15 systemd[1]: demo-node-app.service: Failed with result 'exit-code'.

然后我把服务脚本改成了这个

[Unit]
Description=HTTP Hello World
After=network.target
[Service]
User=mukund
Environment=MY_HOST=localhost
WorkingDirectory=/home/mukund/Work/nodejs/nodeExpTutorial
ExecStart=/usr/bin/pm2 start start config.json
[Install]
WantedBy=multi-user.target

重新加载服务

mukund@mukund-ThinkPad-E15:~/Work/nodejs/nodeExpTutorial$ sudo systemctl daemon-reload

再次启动服务

mukund@mukund-ThinkPad-E15:~/Work/nodejs/nodeExpTutorial$ sudo systemctl start demo-node-app.service

然后在状态上我收到了这条成功消息

mukund@mukund-ThinkPad-E15:~/Work/nodejs/nodeExpTutorial$ sudo systemctl status demo-node-app.service
● demo-node-app.service - HTTP Hello World
     Loaded: loaded (/etc/systemd/system/demo-node-app.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Fri 2020-08-07 19:10:02 IST; 20s ago
    Process: 38359 ExecStart=/usr/lib/node_modules/pm2/bin/pm2 start config.json (code=exited, status=0/SUCCESS)
   Main PID: 38359 (code=exited, status=0/SUCCESS)

Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: [PM2] Applying action restartProcessId on app [app](ids: [ 0 ])
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: [PM2] [app](0) ✓
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: ┌─────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬─────>
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: │ id  │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu >
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: ├─────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼─────>
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: │ 0   │ app       │ default     │ 1.0.0   │ fork    │ 38367    │ 0s     │ 0    │ online    │ 0%  >
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: │ 1   │ config    │ default     │ 1.0.0   │ fork    │ N/A      │ 0      │ 0    │ stopped   │ 0%  >
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: └─────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴─────>
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: [PM2][WARN] Current process list running is not in sync with saved list. Type 'pm2 save' to synch>
Aug 07 19:10:02 mukund-ThinkPad-E15 systemd[1]: demo-node-app.service: Succeeded.

我启动了应用程序,但我无法使用停止命令来停止它

mukund@mukund-ThinkPad-E15:~/Work/nodejs/nodeExpTutorial$ sudo systemctl stop demo-node-app.service
mukund@mukund-ThinkPad-E15:~/Work/nodejs/nodeExpTutorial$ sudo systemctl status demo-node-app.service
● demo-node-app.service - HTTP Hello World
     Loaded: loaded (/etc/systemd/system/demo-node-app.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Fri 2020-08-07 19:10:02 IST; 45s ago
    Process: 38359 ExecStart=/usr/lib/node_modules/pm2/bin/pm2 start config.json (code=exited, status=0/SUCCESS)
   Main PID: 38359 (code=exited, status=0/SUCCESS)

Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: [PM2] Applying action restartProcessId on app [app](ids: [ 0 ])
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: [PM2] [app](0) ✓
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: ┌─────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬─────>
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: │ id  │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu >
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: ├─────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼─────>
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: │ 0   │ app       │ default     │ 1.0.0   │ fork    │ 38367    │ 0s     │ 0    │ online    │ 0%  >
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: │ 1   │ config    │ default     │ 1.0.0   │ fork    │ N/A      │ 0      │ 0    │ stopped   │ 0%  >
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: └─────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴─────>
Aug 07 19:10:02 mukund-ThinkPad-E15 pm2[38359]: [PM2][WARN] Current process list running is not in sync with saved list. Type 'pm2 save' to synch>
Aug 07 19:10:02 mukund-ThinkPad-E15 systemd[1]: demo-node-app.service: Succeeded.

但是你可以看到应用程序仍然处于活动状态,我无法关闭它

有人可以帮我弄清楚问题到底是什么吗? 请帮忙

【问题讨论】:

    标签: pm2 systemctl


    【解决方案1】:

    我在我的 ubuntu 服务器上为非 root 用户加载 pm2 时也遇到了问题,导致“退出代码”失败。被这个命令解决了from here:

    su user -c "pm2 dump && pm2 kill" && su root -c "systemctl daemon-reload && systemctl enable pm2 && systemctl start pm2"
    

    【讨论】: