I've got the following for spawning multiple node processes when user npm starts:
"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?
I suggest you run it with a node_modlue called concurrently that way all processes will run at the same time and it will stop once you tell it to stop.
scripts: {
"start": "concurrently --raw --kill-others \"npm run addition\" \"npm run node2\"",
"addition": "node calculator.addition/index.js",
"division": "node calculator.division/index.js",
...etc
}
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"
}