在Linux中使用PHP的posix_kill()导致奇怪的行为

我正在创build一个网页,将用于监视和控制一些自定义的C程序。 我创build了一个页面,它将启动/停止一个C程序“启动器”(很好的通用名称),然后分叉并创build一些subprocess。 开始工作很好 – exec("cd launcher_dir; nohup ./launcher > outfile 2>&1 &");

停止是有问题的地方。 点击停止button后,随机发生两件事之一。 要么有一个浏览器错误页面(101连接重置或324空响应)或页面加载两次,但你只能看到它的第二次。 我知道它加载两次的原因是因为在代码中debugging消息。 在两种情况下,启动程序进程都被终止(发送SIGTERM)。 但是,如果页面加载两次,第一次杀死启动程序(这部分没有加载页面),第二次检查,发现没有启动进程正在运行,并显示一条消息:“启动程序未运行”。

我正在编写debugging消息到一个文件,发现重新加载发生在PHP代码中有些可变的行(有时某些debugging消息将打印,其他时间不会)。另外,PHP错误报告设置为ALL没有错误给出。

启动器捕获SIGTERM,依次向它的subprocess发送SIGTERM,然后调用exit(0)。

有趣的是,如果SIGKILL被用来杀死启动器,PHP工作正常,但是,这并不能让启动器很好地closures。 这里会发生什么?

这里是相关的PHP代码:

 function stop_launcher(){ $pid = get_launcher_pid(); // Definitely returns the correct pid debug_message("stop_launcher called, pid = ".$pid); if ($pid == "") { // If no 'connection reset' error occurs this is displayed // after first executing the else branch. Why is the php being run twice? print "Launcher doesn't seem to be running.. <br />"; exit; } else { debug_message("killing"); posix_kill(intval($pid), 15); //SIGTERM debug_message("kill finished"); // Sometimes this message is written, sometimes not if (ps_exists($pid)) { // Definitely works. This never gets displayed print "Shutdown failed. Try again</br>"; exit; } } } function debug_message($message){ $fh = fopen(".debug", 'a') or die("can't open file"); fwrite($fh, date("-r").": ".$message."\n"); fclose($fh); } 

任何build议表示赞赏!

事实证明,这是一个让你感到愚蠢的错误! 发射器程序意外地杀死了它的进程组…

启动器是外壳。 如果收到SIGTERM,一些shell会发送SIGTERM给他们的孩子。 改变“nohup”为“exec nohup”应该摆脱这个问题。