简体   繁体   English

杀死一个node.js服务器

[英]Killing a node.js server

I'm have a monitor that auto-daemonizes processes and manages them. 我有一台监视器,可以自动守护进程并对其进行管理。

So I have this node.js program that somehow spawns another process. 因此,我有一个以某种方式产生另一个进程的node.js程序。 My code uses no forking or anything like that, it just has an infinite loop using setInterval that does some db work. 我的代码不使用分叉或类似的东西,它使用setInterval进行一个无限循环,该循环可以执行一些数据库工作。

This is how it's run: 它是这样运行的:

node /path/to/program.js >> program.log 2>&1

After this, top -bcn1|grep program.js shows: 之后, top -bcn1|grep program.js显示:

24763 ubuntu    20   0  695m  21m 5464 S    0  0.3   0:00.39 node /path/to/program.js                                                                                                   
25053 ubuntu    20   0  4264  580  484 S    0  0.0   0:00.01 sh -c node /path/to/program.js >> program.log 2>&1

My process monitor then sets a pid file with 25053, so when I tell it to restart the program it kills 25053, starts the program again, and now the output of the previous command is: 然后,我的进程监视器将一个pid文件设置为25053,因此当我告诉它重新启动程序时,它将杀死25053,再次启动该程序,现在上一个命令的输出为:

24763 ubuntu    20   0  695m  21m 5464 S    0  0.3   0:00.39 node /path/to/program.js                                                                                                   
23520 ubuntu    20   0  630m  21m 5464 S    0  0.3   0:00.39 node /path/to/program.js                                                                                                   
23012 ubuntu    20   0  4264  580  484 S    0  0.0   0:00.01 sh -c node /path/to/program.js >> program.log 2>&1

So how do I make it so the "child" process or whatever it is is also killed? 那么,如何使“子”进程或其任何子进程也被杀死呢?

I have a feeling this is something basic in node but I haven't found an answer yet. 我感觉这是节点中的基本知识,但我还没有找到答案。

you need to listen for the exit event off of process... 您需要监听进程退出事件...

try: 尝试:

var children = [];
//keep track of the child processes...
process.on('exit',function(){
    children.forEach(function(child){
        child.kill('SIGINT');
    });
});

NOTE: in the children, you'll want to listen for 'SIGINT' etc. 注意:在孩子中,您将需要收听'SIGINT'等。

see: http://nodejs.org/api/child_process.html 参见: http : //nodejs.org/api/child_process.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM