此答案只是用于提供entropo节点-FFI溶液的一个例子提出了(见上文)(如所提到的,将在Linux上工作):
这是父进程,它是产卵子,然后退出5秒后:
var spawn = require('child_process').spawn;
var node = spawn('node', [__dirname + '/child.js']);
setTimeout(function(){process.exit(0)}, 5000);
这是子进程(位于child.js)
var FFI = require('node-ffi');
var current = new FFI.Library(null, {"prctl": ["int32", ["int32", "uint32"]]})
//1: PR_SET_PDEATHSIG, 15: SIGTERM
var returned = current.prctl(1,15);
process.on('SIGTERM',function(){
//do something interesting
process.exit(1);
});
doNotExit = function(){
return true;
};
setInterval(doNotExit, 500);
而不current.prctl(1,15)即使父母死亡,孩子也会永远跑步。这里将会用SIGTERM发出信号,这将被优雅地处理。
“断开”注册我认为这会工作。在关闭时,父母被杀,但我确信退出侦听器仍然可以工作。也许我仍然可以[每隔几秒检查一次父进程ID](http://stackoverflow.com/questions/5566009/node-js-parent-process-id),如果它变成了数字1,那就意味着父代已经死了。 – 2011-04-06 12:03:58
如果您正在关闭,您的进程将首先收到“SIGTERM”。你可以捕获这个,并以这种方式进行清理: 'process.on('SIGTERM',function(){ console.log('SIGTERM,exiting ...'); //在这里做一些清理。 ... process.exit(0); });' – entropo 2011-04-06 17:07:44
是的,让孩子定期检查他们的'PPID'是否变成'1',这对父母没有干净地离开的情况来说是一个很好的回退。另请参阅:http:// stackoverflow。com/questions/269494/how-can-i-cause-a-child-process-exit-when-the-parent-does – entropo 2011-04-06 17:25:11