1

I have user submitted java files that the server runs. With the possibility of an infinite loop, I run a timeout countdown in the PHP. The problem is, the proc_get_status seems to update before javaw.exe is actually finished (since it never will be with an infinite loop); procStatus["running"] == false after the first iteration in this:

$javaCmd = "javaw -cp \"$home/$target_dir\" $fl  2>&1 < ". $fileIn;
$proc = proc_open('exec '.$javaCmd, array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w")), $pipes);

$procStatus = proc_get_status($proc);

if($procStatus["pid"] === false)
{
    echo 'PRocess is not running... something is wrong';
}
else
{
    if($procStatus["running"] == true)
    {
        $timeOut = 0;
        while($timeOut < $timeLimit)
        {
            echo 'timeout...'.$timeOut.' while timelimit is '.$timeLimit.'<br/>';
            sleep(1);
            $timeOut = $timeOut +1;
            if( $procStatus["running"] == false) 
            {
                echo 'broke before timeout...<br/>';
                break;  // Exited before the timeout.   
            }
            $procStatus = proc_get_status($proc);
        }
        var_dump($procStatus);  
    }
    echo 'killing process<br/>';

    var_dump(proc_terminate($proc));
}

I have tried to do proc_terminate, proc_close, fcloses on $proc and $pipes, but nothing seems to work in killing the javaw.exe process. Would exec("kill -9 ".$procStatus['pid']) kill successfully? I am testing on windows, but the server is on unix.

Gaʀʀʏ
  • 4,372
  • 3
  • 39
  • 59

1 Answers1

3

Executing exec("kill -9 ".$procStatus['pid']) should definitely kill that process. You could firstly try kill without the '-9' parameter to allow a nicer killing and then maxbe check again if the process is still there, '-9' parameter is to force the kill.

Under Windows (at least Version 7) you could use exec("taskkill /PID ".$procStatus['pid']) for testing, it should do quite the same.

  • `exec("taskkill /PID ".$procStatus['pid'])` worked in killing javaw.exe, but what I dont get, is that after the first iteration of the while loop, `proc_get_status($proc)` updates and thinks that `$procStatus['running'] == false`, meaning that the exec is done running, even when it isn't. – Gaʀʀʏ Aug 20 '12 at 17:55