How to clear child processes on child_process.spawn () in node.js

The following code:

#!/usr/bin/env node "use strict"; var child_process = require('child_process'); var x = child_process.spawn('sleep', [100],); throw new Error("failure"); 

spawns a child process and exits without waiting for the child process to complete.

How can i wait I would call waitpid (2), but child_process does not seem to have waitpid (2).

ADDED:

Sorry, I really need to kill the child process when the parent exists, and not wait for it.

+7
source share
2 answers
 #!/usr/bin/env node "use strict"; var child_process = require('child_process'); var x = child_process.spawn('sleep', [10]); x.on('exit', function () { throw (new Error("failure")); }); 

EDIT:

You can listen to the main process by adding a listener to the main process as process.on('exit', function () { x.kill() })

But an error similar to this problem is a problem, it is better to close process.exit()

 #!/usr/bin/env node "use strict"; var child_process = require('child_process'); var x = child_process.spawn('sleep', [100]); process.on('exit', function () { x.kill(); }); process.exit(1); 
+13
source
 #!/usr/bin/env node "use strict"; var child_process = require('child_process'); var x = child_process.spawn('sleep', [10]); process.on('exit', function() { if (x) { x.kill(); } }); 
+3
source

All Articles