How to kill all child processes on exit?

I think the only way is to keep a reference to the ChildProcess object returned by spawn, and kill it when you exit the 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);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)