在某些情况下无法从PHP停止C守护程序 [英] Cannot stop C daemon in certain situations from PHP

查看:119
本文介绍了在某些情况下无法从PHP停止C守护程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux中创建了一个简单的C守护程序。设置守护程序以捕获SIGTERM信号,进行一些清理并终止。从命令行运行时,其行为符合预期。通过kill命令将SIGTERM发送到守护程序已得到正确处理。

I have created a simple C daemon in Linux. The daemon is setup to catch the SIGTERM signal, do some cleanup and terminate. When run from the command line, this behaves as expected. Sending a SIGTERM to the daemon via the kill command gets handled properly.

但是我希望能够从PHP应用程序启动和停止守护程序。我在PHP中使用exec()进行此操作。要开始

I would however like to be able to start and stop the daemon from a PHP application. I do this using exec() in PHP. To start

exec("$daemon_name");

并停止

exec("kill $daemon_pid");

以这种方式启动守护程序始终有效,但停止则无效。实际上,以这种方式启动时,从命令行执行kill也不起作用。现在只有 kill -9有效,并且显然不能进行所需的清除。据我所知,这个过程只是没有得到信号。

Starting the daemon this way always works, but stopping doesn't. In fact, when started this way, executing kill from the command line also doesn't work. Only "kill -9" now works, and this obviously does not do the required cleanup. As far as I can tell, the process is simply not getting the signal.

这才是真正让我着迷的东西。当我在SLES 12或OpenSuSE 42.2上部署完全相同的配置时,它不起作用,但在OpenSuSE 42.1上,它却起作用(我可以通过PHP启动和停止)。

Here is what really gets me. When I deploy this exact same configuration on SLES 12 or OpenSuSE 42.2, it doesn't work, but on OpenSuSE 42.1, it does work (I can start AND stop via PHP).

这不是权限问题,我对此进行了验证。

It is not a permissions issue, I verified this.

对于导致过程无法接收SIGTERM信号的原因,我没有任何想法。查看 ps aux和 ps -ef的输出,我发现从命令行启动的守护程序和通过PHP启动的守护程序之间没有区别。

I'm out of ideas as to what can cause the process to not receive the SIGTERM signal. Looking at the output of "ps aux" and "ps -ef" I can see no difference between a daemon started from the command line, and one start via PHP.

编辑:
到目前为止,感谢所有评论。他们似乎都没有考虑到完全相同的代码可以在某些系统上运行,而在其他系统上则不行。我唯一能想到的是,守护程序是在不同的环境中启动的。问题:在环境中是否有任何进程启动会导致其忽略信号?

Thanks for all comments so far. None of them seems to take into account that the exact same code works on some systems, but not other. The only thing I can think of, is that the daemon is started in different environments. Question: Is there anything in the environment that a process is started in that can cause it to ignore signals?

推荐答案

也许是您的问题应该是为什么 kill< pid> 可能不起作用的原因。 PHP并不是一个特定的问题。进程很容易忽略 kill 。现在,您提到它是C守护程序,但是,您没有提及该守护程序的作用。因此,我只是假设您的守护进程正在等待I / O,没有正确配置超时,然后将其杀死。

Maybe your question should be why kill <pid> might not work. It is not a certain issue with PHP. kill can easily be ignored by the processes. Now you mention that it is a C daemon, however, you do not mention what the daemon does. So I will just assume that your daemon awaits for I/O, it is not properly configured with a timeout, and you kill it. Then the process will just won't be killed no matter what.

您可以在此处阅读为什么https://askubuntu.com/questions/59811/kill-pid-not-really-killing-the-process-why 解决方案。

You can read here https://askubuntu.com/questions/59811/kill-pid-not-really-killing-the-process-why for possible solutions.

这篇关于在某些情况下无法从PHP停止C守护程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆