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

Bryan Arbelo - MaG3Stican picture Bryan Arbelo - MaG3Stican · Jan 12, 2016 · Viewed 11.8k times · Source

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.

Answer

trex005 picture trex005 · Aug 8, 2018

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.