I'm developing a web site which is accustomed to monitor and control some custom C programs. I have produced a webpage that will start/stop a C program 'launcher' (nice generic title) which in turn forks and produces numerous child processes. The beginning works fine - exec("cd launcher_dir; nohup ./launcher > outfile 2>&1 &");

The preventing is how there is a problem. After clicking the stop button 1 of 2 things happens randomly. Either there's a browser error page (101 Connection Totally reset or 324 Empty Response) or even the page is loaded two times, however, you only view it the 2nd time. The main reason Yes, it loads two times is due to debugging messages within the code. In the two cases the launcher process is wiped out (sent SIGTERM). But when the page loads two times, on the very first time it kills launcher (nothing loads around the page with this part) and also the second time that it inspections and finds that no launcher process is running and shows a note: "Launcher isn't running".

I'm writing debug messages right into a file and located the reload happens in a somewhat variable line within the php code (a certain debug message prints, in other cases it will not.) Also, php error confirming is placed to any or all with no errors receive.

Launcher catches SIGTERM, transmits SIGTERM to it's child processes consequently, after which calls exit().

Oddly enough if SIGKILL can be used to kill launcher, the php works fine so that as expected, however this does not allow launcher to seal lower nicely. What is happening here?

This is actually 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 appreciated!

Launcher may be the spend. Some shells will be sending SIGTERM for their children when they receive SIGTERM. Altering "nohup" to "professional nohup" should eliminate the issue.