anthonyv anthonyv - 25 days ago 8
Node.js Question

Terminating multiple node processes start from NPM run script

I've got the following for spawning multiple node processes when user

npm start

"scripts": {
"start": "node calculator.addition/index.js & node calculator.division/index.js & node calculator.multiply/index.js & node calculator.subtract/index.js &"

This works fine to start multiple node processes but when I command/ctrl c, the processes are still alive. Any ideas how I can correctly exit these processes?

Answer Source

You can also use pm2 to manage your services lifecycle):

pm2 start node calculator.addition/index.js --name addition

This way you can stop all processes with :

pm2 stop all

Your scripts section would look like :

"scripts": {
    "start": "pm2 stop -s addition division;pm2 delete -s addition division; pm2 start node calculator.addition/index.js -n addition; pm2 start node calculator.division/index.js -n division"