Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pm2 - How to start if not started, kill and start if started

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.

like image 926
Bryan Arbelo - MaG3Stican Avatar asked Jan 12 '16 03:01

Bryan Arbelo - MaG3Stican


2 Answers

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.

like image 134
GarryOne Avatar answered Sep 24 '22 03:09

GarryOne


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

like image 43
Hasneet Singh Avatar answered Sep 22 '22 03:09

Hasneet Singh