简体   繁体   English

在不终止子进程的情况下终止node.js程序

[英]Kill a node.js program without killing child processes

I have a node.js program that has started multiple child processes. 我有一个已经启动多个子进程的node.js程序。 I want to be able to test what happens to those child processes if node crashes but leaves them up and running. 我希望能够测试如果节点崩溃但子进程仍在运行的子进程将如何处理。 I tried the following: 我尝试了以下方法:

process.on('SIGUSR1', function(){   process.exit(0);    });

This worked great on my test setup, the child process kept right on running and Node instance shut down when I ran the following command: 这对我的测试设置非常有效,当我运行以下命令时,子进程保持正常运行,Node实例关闭:

killall -10 node

But, much to my surprise on my production deployment, it is failing to leave the child process up. 但是,令我对生产部署感到惊讶的是,它未能使子进程失效。 The main child process of interest to me is an instance of chromium-browser. 我感兴趣的主要子进程是铬浏览器的一个实例。 The difference seems to be the way the process is started. 区别似乎是该过程的启动方式。 In my test setup I was invoking node directly but in my production setup I was calling a script that did the invoking. 在测试设置中,我直接调用节点,但是在生产设置中,我调用了执行调用的脚本。

What I would like to find is a way inside the anonymous callback function above that handles the SIGUSR1 signal to tell node not to shut down its child processes when it kills itself. 我想找到的是上面的匿名回调函数中的一种方法,该方法处理SIGUSR1信号,告诉节点在杀死自己的子进程时不要关闭其子进程。 I know I can go detach the process that I created but I'd rather do something less involved (even though admittedly, that's not too involved). 我知道我可以分离我创建的过程,但是我宁愿做些不那么复杂的事情(即使可以承认,也不太复杂)。

I'm running node 0.8.21 on Ubuntu 12.04. 我在Ubuntu 12.04上运行节点0.8.21。

Thanks for any ideas you can throw my way. 感谢您提出的任何想法。

I went ahead and tried to see how much grief just creating the process detached would cause me, whether that would still allow me to use signal callbacks etc and all seems well. 我继续尝试查看仅创建分离的进程会给我造成多大的痛苦,是否仍然允许我使用信号回调等,而且一切似乎都很好。 So I'm just going to implement it that way. 因此,我将以这种方式实施它。

Here was my original code: 这是我的原始代码:

self.oUIProcessHandle = spawn('chromium-browser', 
    ['http://127.0.0.1:8091/index.html', '-kiosk', '-incognito', '-start-maximized']);

I changed it to the following: 我将其更改为以下内容:

self.oUIProcessHandle = spawn('chromium-browser', 
    ['http://127.0.0.1:8091/index.html', '-kiosk', '-incognito', '-start-maximized'],
    {detached: true, stdio: ['ignore', 'ignore', 'ignore']});

Now when I call process.exit(0) from my code, it leaves my browser up and running. 现在,当我从代码中调用process.exit(0) ,它将使我的浏览器正常运行。

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

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