pcntl_signal 不是由子进程触发的

发布于 2024-11-06 23:04:10 字数 846 浏览 4 评论 0原文

我想实现这一点:

如果守护进程收到 SIGHUP 信号,则再次运行该进程(作为子进程)并杀死父进程。

当我运行它时,第一次正在工作:

> php test.php
> kill -HUP pid
> ps -ef |grep test.php
> result:... newPID test.php

问题是,如果现在我正在终止子进程,则不会触发

> kill -HUP newPID
> ps -ef |grep test.php
> result: ... newPID(the same) test.php

该函数代码:

test.php:
<?php
   declare(ticks = 1);

   $mypid = posix_getpid();

   function sig_handler()
   {

   Global $mypid;
   echo "Received sighup, we need to reload ourselves now \n";
   echo "mypid:$mypid \n";
   system("(php test.php)>/dev/null &");

   posix_kill($mypid,9);

   }

   pcntl_signal(SIGHUP,  "sig_handler");

   sleep(500);

   ?>

此代码适用于 PHP 5.2.9,但不适用于 PHP 5.3.5 。有什么办法让它在那个版本上也能工作吗?

谢谢!

罗尼

I want to achieve this:

if the daemon gets SIGHUP than run the process again (as child) and kill the parent.

When i'm running it, the first time is working:

> php test.php
> kill -HUP pid
> ps -ef |grep test.php
> result:... newPID test.php

The problem is that if now i'm killing the child process,the function is not triggered

> kill -HUP newPID
> ps -ef |grep test.php
> result: ... newPID(the same) test.php

The code:

test.php:
<?php
   declare(ticks = 1);

   $mypid = posix_getpid();

   function sig_handler()
   {

   Global $mypid;
   echo "Received sighup, we need to reload ourselves now \n";
   echo "mypid:$mypid \n";
   system("(php test.php)>/dev/null &");

   posix_kill($mypid,9);

   }

   pcntl_signal(SIGHUP,  "sig_handler");

   sleep(500);

   ?>

This code works on PHP 5.2.9 but not on PHP 5.3.5. Is there any way to make it works also on that version?

Thanks!

Ronny

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

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

发布评论

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

评论(2

沙沙粒小 2024-11-13 23:04:10

PHP 中的信号处理程序是不可重入的。因此,您从信号处理程序中派生/生成的任何进程本身都无法响应信号。我很想了解该代码在 PHP 5.2.9 中是否有效的详细信息,因为据我所知,这种行为是一致的。

那么你能做什么呢?

在许多情况下,最好的选择是在应用程序中定期轮询的信号处理程序内设置 $signal_recd 标志。

我是一个相当流行的 PHP 守护程序库的作者,https://github.com/shaneharter/PHP- Daemon

如果我是你,我会在此基础上构建你的应用程序并让它担心自动重新启动等问题。但如果没有别的事情,你可能可以看看它的 Core_Daemon::auto_restart() 和信号处理代码。

Signal handlers in PHP are not re-entrant. So any process you fork/spawn from within a signal handler cannot itself respond to signals. I'd love to see details if that exact code worked in PHP 5.2.9 because to my knowledge this behavior has been consistent.

So what can you do?

In many cases, the best option is to set a $signal_recd flag inside the signal handler that you poll periodically in your application.

I'm the author of a fairly popular PHP Daemon Library, https://github.com/shaneharter/PHP-Daemon

If i were you, I would just build your app on that and let it worry about auto-restarting, etc. But if nothing else, you can probably look at it's Core_Daemon::auto_restart() and signal handling code.

雨的味道风的声音 2024-11-13 23:04:10

我怀疑这是 PHP 中的一个错误。我仍在尝试绝对确定地确认,但就目前情况而言,我们遇到了同样的问题。

如果启动子进程并发送信号,子进程将接收并处理该信号。如果您随后从父脚本重新启动另一个子进程并跟进另一个信号,则该子进程不会收到第二个信号。我在 PHP 5.3.10 中进行了调整、摆弄和努力寻找解决此问题的方法,但无济于事。

My suspicion here is that this is a bug in PHP. I'm still trying to confirm with absolute certainty, but as it stands we are having the same issue.

If you launch a child process and send a signal, the child process will receive and handle the signal. If you subsequently re-launch another child process from the parent script and follow up with another signal, the second signal is not received by the child process. I've tweaked, twiddled, and toiled to find a way around this issue in PHP 5.3.10 to no avail.

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