如何通过 systemctl 停止 pm2 进程

如何通过 systemctl 停止 pm2 进程

这是我的服务文件

[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.

但正如你所见,应用程序仍然处于活动状态,我无法关闭它

有人能帮我弄清楚到底是什么问题吗?

答案1

您应该ExecStop在服务文件的[Service]部分下添加一条指令,其中包含用于停止服务的适当命令。它可能类似于:

ExecStop=/usr/bin/pm2 stop app_name

根据您的需要更改语法(我发现它这里)。

相关内容