Cannot stop C daemon in certain situations from PH

2019-09-09 20:31发布

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.

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");

and to stop

exec("kill $daemon_pid");

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.

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.

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.

Edit: 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?

标签: php c linux daemon
1条回答
闹够了就滚
2楼-- · 2019-09-09 20:38

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.

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

查看更多
登录 后发表回答