检测父进程何时退出
我将有一个用于处理网络服务器重新启动的父进程。它将向子级发出信号以停止侦听新请求,子级将向父级发出信号以表明其已停止侦听,然后父级将向新子级发出信号以表明其可以开始侦听。通过这种方式,我们可以实现该级别重新启动的停机时间少于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一个更简单的解决方案可能是在子进程中注册“断开连接”
a simpler solution could be by registering for 'disconnect' in the child process
这个答案只是为了提供 entropo 提出的 node-ffi 解决方案的一个例子(上面)(如上所述,它将在 Linux 上工作):
这是父进程,它正在生成子进程,然后在 5 秒后退出:
这个是没有 current.prctl(1,15) 的子进程(位于 child.js 中),
即使父进程死亡,子进程也会永远运行。此处将使用 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:
this is the child process (located in child.js)
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.
您能否在父进程中放置一个 退出侦听器向孩子们发出信号?
编辑:
您还可以使用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 )
我作为后台工作者从本机 OSX 应用程序中启动 Node.JS。为了让使用 node.js stdout 的父进程死亡/退出时 node.js 退出,我执行以下操作:
就这么简单,但我不确定这是否是您一直在要求的;-)
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:
Easy like that, but I'm not exactly sure if it's what you've been asking for ;-)