Spawn and kill a process in node.js

Refer to this discussion

Once you start listening for data on stdin, node will wait for the input on stdin until it is told not to. When either user presses ctrl-d (meaning end of input) or the program calls stdin.pause(), node stops waiting on stdin.

A node program does not exit unless it has nothing to do or wait for. Whats happening is, it is waiting on stdin and therefore never exits.

Try changing your setTimeout callback to

console.log('kill');
child.stdin.pause();
child.kill();

I hope that should work.

Leave a Comment