Reputation: 2224
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.
Upvotes: 24
Views: 29782
Reputation: 472
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
Upvotes: 2
Reputation: 1
# 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.
Upvotes: 0
Reputation: 373
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).
Upvotes: 8
Reputation: 1488
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.
Upvotes: 12
Reputation: 5115
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.
Upvotes: 8