Sid Sid - 6 months ago 31
PHP Question

PHP script stuck on exec even after running command in backgroud on Windows

I want a php script from which I can execute a program, and terminate it if it doesn't complete execution in 2 seconds. I am using Windows. I have tried the following code:

exec("start /B program.exe");
sleep(2);
exec('taskkill /F /IM "program.exe"');


This doesn't seem to work as script is stuck on the first
exec
statement as long as
program.exe
is not finished execution. I can't figure out how to do fix this issue.

Answer

Are you doing this with php cli (command line)? Open a command prompt as administrator.
To not being blocked by waiting for the program close the process of opening the program.

php myscript.php

pclose(popen("start /B program.exe", "r"));
sleep(2);
exec('taskkill /F /IM program.exe');
exit(0);

Would also be fine to put the exec start into a separate script and fire this script using exec

Comments