-2

I want to write a node script that detects if another node script is running.

My question is (all running on node in Windows 7), within a node script, how do I detect if another node script is running?

I'm new to node and I couldn't find an answer that matched my question exactly.

Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
Heath
  • 1,021
  • 3
  • 12
  • 19
  • Can you expand on your question, please? For example, what is your Node Server - is it Windows environment or Linux? Also - why are you wanting to check - is it, for example - say an Express app that you want to kill beforehand before restarting? – ash Aug 01 '16 at 18:53
  • I'm running in Windows. The purpose of this is, I have a script on my local computer that host a react website I'm building. I often need to switch between production and development environments. This is done by stopping the script that's hosting the website. Then making a couple changes to a config.js file. Then restarting script that host the website. I make these changes enough that I want to create a script that will stop the script, change the config.js file, then restart script. – Heath Aug 01 '16 at 22:32

1 Answers1

0

I'd be tempted to use nodemon...

Once installed (with npm install -g nodemon), instead of starting your server with say node app.js - start it with nodemon app.js.

Nodemon will listen for any changes to your files, and restart your server automatically for you. Perfect for development!

If this, for some reason, is not an option though, you could use something like the taskkill command to kill all the node processes with something like taskkill /F /IM node.exe

I'm not a Windows Guru though, so there's will likely be a better way of doing this with something like a batch script or windows service (rather than just killing ALL node apps)

Perhaps someone on StackOverflow with more Windows experience can help out here.

Good luck!

ash
  • 1,224
  • 3
  • 26
  • 46