Im using npm scripts, I have some of them that should run in parallel. I've got something like this:
...
scripts: {
"a": "taskA &",
"preb": "npm run a",
"b": "taskB"
}
...
This is fine! But I'd like to kill automatically taskA running the background after taskB has finished.
How can I do that? Thanks!
The
npm-run-all
package may be what you're looking for:Then in your
package.json
file:(
-p
runs them in parallel, use-s
for sequential.)I don't believe npm is the best tool to manage complex relationships between processes.
You would be far better served by creating a node script that uses node's
child_process
module to manage the launching and killing of co-processes, perhaps usingspawn
.Having said that, and in the spirit of always trying to provide a direct, usable answer..
You could structure your npm scripts like (assumes bash shell):
The only 'magic' here is that:
bash
shell (which is what happens when you add&
to the end), you can discover it PID using$!
, but only immediately after the command is run. (See Advanced Bash-Scripting Guide: Chapter 9. Another Look at Variables - 9.1. Internal Variables for its description.)&&
test is handled correctly. (See this answer for more information.)Using the concurrently package looks like a less tricky & painful way. This avoids detached processes (
&
) alltogether. And promises to be more cross-plattform, too.and then in package.json
Tested (under Ubuntu 16.04, npm 5.6), also see here.