How to stop a pm2 process from systemctl

Here is my service file

[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

where config.json is

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

when i run this command i am getting this error

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

then i changed the service script to this

[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

reloaded the serveice

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

started the service again

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

then on the status i got this success message

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.

I got the app started but i cannot use the stop command to stop it

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.

but as you can see the app is still active, i cannot shut it down

can someone help me in figuring out what the issue actually is?

1 Answer

You should add an ExecStop directive in your service file, under the [Service] section, with the appropriate command to stop the service. It may be like:

ExecStop=/usr/bin/pm2 stop app_name

Change the syntax according to your needs (I found it here).

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like