All you have to do here is attach a handler for the stream's end
event. Once that is fired, you can kill your spawned process.
If this sub-process is something you wrote (such as in Node.js), you can have the process kill itself. Otherwise, just have the same chunk of code that spawns it in the first place add a handler for end
, and kill it.