node.js - How to kill all child processes on exit? -
how kill child processes (spawned using child_process.spawn) when node.js process exit?
i think way keep reference childprocess
object returned spawn
, , kill when exit master process.
a small example:
var spawn = require('child_process').spawn; var children = []; process.on('exit', function() { console.log('killing', children.length, 'child processes'); children.foreach(function(child) { child.kill(); }); }); children.push(spawn('/bin/sleep', [ '10' ])); children.push(spawn('/bin/sleep', [ '10' ])); children.push(spawn('/bin/sleep', [ '10' ])); settimeout(function() { process.exit(0) }, 3000);
Comments
Post a Comment