2
father.js
var spawn = require('child_process').spawn;
var child = spawn('node',['child.js']); 

setInterval(function(){
  child.kill('SIGINT');
},2000);

child.on('exit',function(code,signal){
 console.log('process exit '+code+' '+signal);
});

child.js
process.stdin.resume();
process.on('SIGINT', function() {
  console.log('Got SIGINT. Press Control-D to exit.');
});

节点版本:0.10.17

为什么子进程无法捕获“SIGINT”?但如果你单独运行 node child.js,它可以捕获Ctrl+C终止 cmd 的信号。

4

1 回答 1

2

它确实得到了SIGINT!只是你没有在听子进程的输出。在father.js中加入这一行来查看。

child.stdout.pipe(process.stdout);
于 2013-08-26T06:03:37.063 回答