Using PHP posix_kill () on Linux causing strange behavior

I create a web page that will be used to monitor and manage some C user programs. I created a page that starts / stops the Launcher program (a beautiful generic name), which then hangs and creates several child processes. The original works fine - exec("cd launcher_dir; nohup ./launcher > outfile 2>&1 &");

Stop where there is a problem. After pressing the stop button, one of two things happens randomly. Either a page with a browser error (101 Connection Reset, or 324 Empty Response), or the page loads twice, but you only see it the second time. The reason I know it loads twice is debugging messages in code. In both cases, the startup process starts (SIGTERM is sent). But if the page loads twice, the first time it kills the launcher (nothing loads on the page for this part), and the second time it checks and finds out that the launch process is not started and the message β€œLauncher does not work” is displayed.

I write debug messages to a file and find that the reboot occurs on a slightly variable line in php code (sometimes a certain debug message may be printed, otherwise it will not.) Also a php error report is set for ALL and no errors not indicated.

Launcher catches SIGTERM, sends SIGTERM to it the child processes in turn, and then calls exit (0).

Interestingly, if SIGKILL is used to destroy the launcher, php works fine and, as expected, however, this prevents the launcher from closing normally. What could be here?

Here is the relevant PHP code:

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

Any suggestions are welcome!

+4
source share
2 answers

It turns out that this was one of those mistakes that make you feel stupid! The launcher accidentally started its process group ...

+2
source

Launcher is a shell. Some shells will send SIGTERM to their children if they receive SIGTERM. Changing "nohup" to "exec nohup" should get rid of the problem.

0
source

Source: https://habr.com/ru/post/1391349/


All Articles