pm2 - How to start if not started, kill and start if started
Asked Answered
H

6

24

I am trying to start pm2 if it is not running, or kill it and start if it is, how can I achieve this behavior in the WINDOWS command line interface?

There are plenty of solutions using grep in linux but nothing for windows, any idea on how to get this behaviour?

The documentation says that pm2 start -f app.js will kill and start the app but it actually just creates another instance.

Hemorrhoidectomy answered 12/1, 2016 at 3:13 Comment(2)
why not just issue a kill anyway?Quahog
Please read this link, there are a lot of cmds, github.com/Unitech/pm2Cesar
L
12

Use this:

pm2 delete main.js 2> /dev/null &&  pm2 start main.js

This part: 2> /dev/null - will simply redirect the stderr to the /dev/null, meaning to nowhere.

Limbourg answered 27/3, 2020 at 22:4 Comment(1)
This will fail the script if it's not running, try: pm2 delete main.js 2> /dev/null || trueReciprocate
R
8

It does not seem there is a "single command" way to do this, which is rather important in many development environments, so here are some options:

put soyuka's suggestion on one line.

pm2 stop myprocess; pm2 start myprocess.js

This will output errors, but it will work.

They also have this option built into their ecosystem tools. To use this, go into the folder you are working with and run

pm2 ecosystem

This will generate a file ecosystem.config.js which you will need to make sure your name and script are correct within.

You can then use the command:

pm2 startOrReload ecosystem.config.js

I, however also want to see my logging, so I use this command:

pm2 flush && pm2 startOrReload ecosystem.config.js && pm2 log

This will also flush the logs so you are not seeing old logs.

Reinhard answered 8/8, 2018 at 5:9 Comment(0)
R
8

You can do something like this

pm2 delete your_app_name || : && pm2 start index.js -i 1 --name 'your_app_name'

The : is a null operator that returns 0 success exit code. So whatever happens, pm2 start command will execute (even if pm2 delete fails, for the case where the app does not exist yet).

Rowen answered 9/1, 2021 at 7:6 Comment(0)
B
3

I'd do this :

pm2 stop myprocess.js #this will just say process not found
pm2 start myprocess.js

Or if you want to clear everything :

pm2 kill
pm2 stop

If you want more advanced possibilities check out the pm2 api.

Bohun answered 12/1, 2016 at 20:28 Comment(2)
Hi, the process not found part lead to an error that prevent the next bit of my script from executing...Sennit
try pm2 stop myprocess.js || true - so the controlling script never sees any error codeInattention
J
2

if you execute a bash script that call pm2, you can consider using :

pm2 stop $SERVICE_NAME || true

This will prevent pm2 returning -1 if $SERVICE_NAME does not exist

Janel answered 12/4, 2023 at 9:53 Comment(0)
W
0
# checking if 'appname' running in pm2

PM2_EXIST=$(if pm2 list 2> /dev/null | grep -q appname; then echo "Yes" ; else echo "No" ; fi)

if [ $PM2_EXIST = Yes ] ; then
    pm2 restart appname
    echo "Restart appname."
else
    pm2 start file.js --name appname
    echo "Started appname."
fi

This script checks if there is a PM2 process with the name "appname", if so it restarts the process, if not it recreates the process.

You can change the actions inside the if or else.

Hope its helps.

Wendt answered 26/3, 2023 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.