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 using spawn
.
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):
scripts:{
runBoth: "npm runA & npm runB", // run tasks A and B in parallel
runA: "taskA & TASKA_PID=$!", // run task A and capture its PID into $TASKA_PID
runB: "taskB && kill $TASKA_PID" // run task B and if it completes successfully, kill task A using its saved PID
}
The only 'magic' here is that:
- when you run a command in the background using the
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.)
- taskB must call process.exit(0) on success or process.exit(-1) on failure so that the
&&
test is handled correctly. (See this answer for more information.)