PHP - proc_get_status 'running' returning false when javaw executable still running

StackOverflow https://stackoverflow.com/questions/12043159

  •  27-06-2021
  •  | 
  •  

Question

I am executing javaw with an infinite looping java file using this:

$descriptorspec = array(
                                0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
                                1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
                                2 => array("pipe", "w") // stderr is a file to write to
                        );

 // e.x. $javaCmd = "java -cp "../Fully Diversified Sequences of Sets" SuperEasyProblemSolution2 > /dev/null 2>&1 < h.in"
$proc = proc_open($javaCmd, $descriptorspec, $pipes);//, $cwd, $env);
 stream_set_blocking($pipes[0], 0) ;

 $status = proc_get_status($proc);  
 var_dump($status);
  $timeOut = 5;
  $currentSecond = 0;

  while( $currentSecond < $timeOut ) {
        echo '<br/>';
        sleep(1);
        $currentSecond = $currentSecond +1;
        if( ! $status["running"] )
        {
            echo 'process exited before timing out'; // Process must have exited, success!
            return;
        }
        $status = proc_get_status($proc);
        var_dump($status);
  } // end while
  if($currentSecond  == $timeOut)
  {
      // kill KILL KILL!
      exec("taskkill /PID ".$status['pid']);
  }

On the first call to proc_get_status, running attribute returns true. On the second call (one second later) to proc_get_status, running returns false. The application javaw.exe is still running, however ( I call proc_get_status in a while loop that will eventually timeout.)

My goal is to taskkill the program after the timeout has expired. See a similar question here. I am running on Win7 64 bit, PHP 5.3

Var dump on $status: (Note; I tried applying stream_set_blocking($pipes[0], 0) ;, same issue)

Before entering timeout loop:

    array(8) { 
["command"]=> string(157) "java -cp "../Fully Diversified Sequences of Sets" SuperEasyProblemSolution2 /dev/null 2>&1 < h.in" 
["pid"]=> int(3264) 
["running"]=> bool(true) 
["signaled"]=> bool(false) 
["stopped"]=> bool(false) 
["exitcode"]=> int(-1) 
["termsig"]=> int(0) 
["stopsig"]=> int(0) 
} 

After first iteration/sleep(1):

    array(8) { 
["command"]=> string(157) "java -cp "../Fully Diversified Sequences of Sets" SuperEasyProblemSolution2 /dev/null 2>&1 < h.in" 
["pid"]=> int(3264) 
["running"]=> bool(false) 
["signaled"]=> bool(false) 
["stopped"]=> bool(false) 
["exitcode"]=> int(1) 
["termsig"]=> int(0) 
["stopsig"]=> int(0) 
} 
    process exited before timing out

After testing, it appears that $status['pid'] is different than the pid for javaw.exe under Windows' Resource Monitor.

Was it helpful?

Solution

Although we couldnt pinpoint the issue with proc_get_status returning the incorrect boolean, we were able to find a good work-around (for Unix), using

if (file_exists("/proc/".$status["pid"])) { // process still running }

to check if the process was still running or not.

Here is the full code excerpt:

$proc = proc_open($javaCmd, array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w")), $pipes);

$status = proc_get_status($proc);

if($status["pid"] === false)
{
    // Process did not execute correctly
}
else
{
        $timeOut = 0;
        $forceKill = true;

        while($timeOut < $timeLimit)
        {
            $timeOut++;
            sleep(1);
            echo 'Timeout is at '.$timeOut.' and timelimit: '.$timeLimit.'<br/>';

            if (file_exists("/proc/".$status["pid"])) {
            // process still running
            }
            else
            {
                echo 'Finished running after '.$timeOut.' seconds<br/>';
                $forceKill = false;
                break;
            }

        }
        if($forceKill == true)
        {
            echo ' Manual killing pid '.$status['pid'];
            exec("sudo kill ".$status['pid']);
            $runTime = $timeLimit;
        }


}

OTHER TIPS

If the Java app is running as a daemon, then it would have a different PID.

Does the Java app attempt to output anything? If so - then you aren't reading the pipe so the script will eventually fill up the buffer and hang (which might cause Java to stop running)

If you don't care about the output then send it to /dev/null to get rid of it:

/cmd/to/run > /dev/null 2>&1

If you choose to read the data from the pipe, the read function will block if there is no data - to get around that you might want to use:

stream_set_blocking($pipes[0], 0)

This will turn off blocking mode (the read function will return FALSE immediately if there is no data waiting).

Since you mentioned you are doing this on windows I realized what is happening-

javaw (with a W) starts it as a windows app and returns immediately (which is why running=false)

you need to use java (without the w) to run it in console.

java = console application
javaw = windows application with no console
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top