如何杀死它并行运行的子进程

时间:2013-09-05 06:43:06

标签: multithreading node.js asynchronous child-process

我催生了很多孩子

var spawn = require('child_process').spawn,
  newProcess1 = spawn('node', ['File1.js']),
  newProcess2 = spawn('node', ['File2.js']),
  newProcess3 = spawn('node', ['File3.js']);

将并行运行。如果newProcess1失败,则必须停止或杀死newProcess2和newProcess3(如果尚未启动)。

如何杀死子进程?

1 个答案:

答案 0 :(得分:0)

newProcess1.on('close', function (code) { 
  console.log('child process ' + newProcess1.pid + ' exited with code ' + code); 
  newProcess2.kill();
  newProcess3.kill();
});

http://nodejs.org/api/child_process.html