Stop php exec() using set_time_limit

I can stop

exec("Ping www.google.com");

Using "ini_set('max_execution_time', 5)" or "set_time_limit(5)" but not

exec("java myclass"); //infinite Loop class

why? and how I can stop the exec()?

let's say the java class that I want to run contain:

for(int A = 0; A == 0;)
{
    System.out.println(A + " ");
}

how to stop them using PHP?

Note: I can't edit the java file (I also want to run different class that doesn't infinite running)

Answers


you can try to create one exec.php file place your code inside

<?php
ini_set('max_execution_time', 5)
exec("java myclass");
?>

and where you need execute exec("PATH/exec.php") instead of exec("java myclass");


As documentation explains, that's just not portable:

The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.

Yet in practice I've found that Windows doesn't necessarily work this way. So rather than non-portable I'd say it's just not possible.

You'll have to use more advanced Process Control Extensions, typically something like PCNTL.


You can use the proc_ functions to get better control. You will find it in the manual. Below you find code you might find useful. It works only under windows, you need a different kill routine on linux. The script terminates the (else endless running) ping process after approximatly 5 seconds.

<?php
function kill($pid){ 
    return stripos(php_uname('s'), 'win')>-1  ? exec("taskkill /F /T /PID $pid") : exec("kill -9 $pid");
}

$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("file", "tmp/error-output.txt", "a+") // stderr is a file to write to
);

$process = proc_open("Ping www.google.com -t",$descriptorspec,$pipes);

$terminate_after = 5; // seconds after process is terminated

usleep($terminate_after*1000000); // wait for 5 seconds

// terminate the process
$pstatus = proc_get_status($process);
$PID = $pstatus['pid'];

kill($PID); // instead of proc_terminate($resource);
fclose($pipes[0]); 
fclose($pipes[1]); 
proc_close($process);

$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo 'Process terminated after: '.$time;

Need Your Help

Select interval of time series object by date and time

r date time-series hour

My question is about how to manage the dates and times in an air quality database, which saved data every ten minutes all day, every day from 2002 through 2008.

ASP.NET MVC 5 Html.DropDownListFor with datetime doesn't work SelectList selected

c# asp.net-mvc datetime razor asp.net-mvc-5

I have a property with type Datetime and in the edit view the user select this property with a Html.DropDownListFor , the problem is that the Html.DropDownListFor doesn't get the property Selected in