检测父进程何时退出

发布于 2024-10-30 00:14:35 字数 285 浏览 1 评论 0原文

我将有一个用于处理网络服务器重新启动的父进程。它将向子级发出信号以停止侦听新请求,子级将向父级发出信号以表明其已停止侦听,然后父级将向新子级发出信号以表明其可以开始侦听。通过这种方式,我们可以实现该级别重新启动的停机时间少于 100 毫秒(我也有零停机孙子重新启动,但这并不总是足以重新启动)。

当需要关闭时,服务管理器将杀死父进程。孩子如何检测到父母已经结束了?

使用子进程的 stdin 和 stdout 发送信号。也许我可以检测到标准输入流的结尾?我希望避免轮询间隔。另外,如果可能的话,我希望这是一个非常快速的检测。

I will have a parent process that is used to handle webserver restarts. It will signal the child to stop listening for new requests, the child will signal the parent that it has stopped listening, then the parent will signal the new child that it can start listening. In this way, we can accomplish less than 100ms down time for a restart of that level (I have a zero-downtime grandchild restart also, but that is not always enough of a restart).

The service manager will kill the parent when it is time for shutdown. How can the child detect that the parent has ended?

The signals are sent using stdin and stdout of the child process. Perhaps I can detect the end of an stdin stream? I am hoping to avoid a polling interval. Also, I would like this to be a really quick detection if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剑心龙吟 2024-11-06 00:14:35

一个更简单的解决方案可能是在子进程中注册“断开连接”

process.on('disconnect', function() {
  console.log('parent exited')
  process.exit();
});

a simpler solution could be by registering for 'disconnect' in the child process

process.on('disconnect', function() {
  console.log('parent exited')
  process.exit();
});
ι不睡觉的鱼゛ 2024-11-06 00:14:35

这个答案只是为了提供 entropo 提出的 node-ffi 解决方案的一个例子(上面)(如上所述,它将在 Linux 上工作):

这是父进程,它正在生成子进程,然后在 5 秒后退出:

var spawn = require('child_process').spawn;
var node = spawn('node', [__dirname + '/child.js']);
setTimeout(function(){process.exit(0)}, 5000);

这个是没有 current.prctl(1,15) 的子进程(位于 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);

即使父进程死亡,子进程也会永远运行。此处将使用 SIGTERM 发出信号,该信号将得到妥善处理。

This answer is just for providing an example of the node-ffi solution that entropo has proposed (above) (as mentioned it will work on linux):

this is the parent process, it is spawning the child and then exit after 5 seconds:

var spawn = require('child_process').spawn;
var node = spawn('node', [__dirname + '/child.js']);
setTimeout(function(){process.exit(0)}, 5000);

this is the child process (located in 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);

without the current.prctl(1,15) the child will run forever even if the parent is dying. Here it will be signaled with a SIGTERM which will be handled gracefully.

暮色兮凉城 2024-11-06 00:14:35

您能否在父进程中放置一个 退出侦听器向孩子们发出信号?

编辑:
您还可以使用node-ffi(节点外部函数接口)来调用...
prctl(PR_SET_PDEATHSIG, SIGHUP);
...在Linux中。 ( man 2 prctl )

Could you just put an exit listener in the parent process that signals the children?

Edit:
You can also use node-ffi (Node Foreign Function Interface) to call ...
prctl(PR_SET_PDEATHSIG, SIGHUP);
... in Linux. ( man 2 prctl )

蓝眼泪 2024-11-06 00:14:35

我作为后台工作者从本机 OSX 应用程序中启动 Node.JS。为了让使用 node.js stdout 的父进程死亡/退出时 node.js 退出,我执行以下操作:

// Watch parent exit when it dies

process.stdout.resume();
process.stdout.on('end', function() {
  process.exit();
});

就这么简单,但我不确定这是否是您一直在要求的;-)

I start Node.JS from within a native OSX application as a background worker. To make node.js exit when the parent process which consumes node.js stdout dies/exits, I do the following:

// Watch parent exit when it dies

process.stdout.resume();
process.stdout.on('end', function() {
  process.exit();
});

Easy like that, but I'm not exactly sure if it's what you've been asking for ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文