如何杀死nodejs中的子进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20187184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to kill childprocess in nodejs?
提问by Deepak Patil
Created a childprocess using shelljs
使用shelljs创建了一个子进程
!/usr/bin/env node
require('/usr/local/lib/node_modules/shelljs/global');
fs = require("fs");
var child=exec("sudo mongod &",{async:true,silent:true});
function on_exit(){
console.log('Process Exit');
child.kill("SIGINT");
process.exit(0)
}
process.on('SIGINT',on_exit);
process.on('exit',on_exit);
Child process is still running .. after kill the parent process
子进程仍在运行 .. 杀死父进程后
回答by Michael Tang
If you can use node's built in child_process.spawn, you're able to send a SIGINTsignal to the child process:
如果您可以使用节点的内置child_process.spawn,则可以向SIGINT子进程发送信号:
var proc = require('child_process').spawn('mongod');
proc.kill('SIGINT');
An upside to this is that the main process should hang around until all of the child processes have terminated.
这样做的好处是,主进程应该一直运行,直到所有子进程都终止。

