Reputation: 6955
I have the following bash script:
node ./build.js & node ./server.js
It creates two nodejs processes and executes them in parallel. When I press Ctrl-C
, both processes are terminated.
I'm trying to do the same with Windows cmd shell. Here's what I have currently:
start /B node ./build.js & node ./server.js
It successfully starts the same two processes, but after I press Ctrl-C
it kills only the second one; the node ./build.js
remains active and I have no easy way to terminate it from the console.
Sadly PowerShell is not an option here, because these scripts should run as part of npm scripts, and it could use only cmd as Windows shell.
Upvotes: 4
Views: 1083
Reputation: 2252
A cross-platform solution (Win, Linux, Mac) to running multiple processes from an npm script is to install the parallelshell npm library.
npm install parallelshell --save-dev
Then in your package.json
file you can have define the script as follows:
"scripts": {
"myScript" : "parallelshell \"node ./build.js\" \"node ./server.js\""
}
To execute npm run myScript
. Or even quicker - call the script start
instead of myScript
and execute npm start
.
NB - Windows doesn't recognise single quotes hence the escaped double quotes in the script.
Upvotes: 1
Reputation: 1362
Even if you can't run PowerShell commands directly from node, as long as you can run cmd commands, you can use powershell.exe to execute anything. In essence:
powershell -Command "Get-Item ."
Now, to the actual solution:
powershell -Command "Start-Process -NoNewWindow node .\server1.js; Start-Process -NoNewWindow node .\server2.js"
The command above should launch both server1 and server2 and Ctrl+C or equivalent would terminate both child processes.
Upvotes: 4