Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

limit execution time of shell_exec

I have an exe file which has the following code:

while(1)

printf("hello\n");

I'm executing this exe through php using shell_exec

$output = shell_exec('C:/Users/thekosmix/Desktop/hello.exe 2>&1');

echo $output;

now the script is executing for very long time untill i kill the process from task manager and it gives fatal error:

(Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 133693440 bytes) in C:\xampp\htdocs\shell\index.php on line 7)

I want the script (or this function) to run for a given time duration and print whatever output is generated during the time-duration not any fatal error. set_time_limit() is also not solving the problem.

like image 555
thekosmix Avatar asked Oct 08 '22 00:10

thekosmix


1 Answers

You can't do this from within PHP - set_time_limit() is only checked after each PHP statement gets executed. Under linux you'd use ulimit, but it looks like you're using Windows.

You'll need to either modify the hello.exe executable to build in a timeout, or write a wrapper that you call from PHP, which call hello.exe and handles the timeout. In any language that can easily fork, this is trivial.

like image 178
Cal Avatar answered Oct 12 '22 10:10

Cal