Long background process in PHP on Windows machine
I have a script in PHP that uses Curl to find a long list of urls one by one and write the response to a file at the end, I want the list to be loaded from the browser and left (without making the user wait for a response). I have already tried the following solutions -
$command = "C:\wamp\bin\php\php5.5.12\php.exe ../background_process/subscribe_bg.php ".$file_temp_path;
shell_exec(sprintf('%s > /dev/null 2>/dev/null &', $command));
Executes the script successfully, but makes the browser wait. (This will probably run in the background on a Linux machine.)
$command = "C:\wamp\bin\php\php5.5.12\php.exe ../background_process/subscribe_bg.php ".$file_temp_path;
execInBackground($command);
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
I found this solution for Windows machine, but doesn't work for me. the script is not executed at all.
Please suggest a best practice for running a long process (not very long ~ 30-40 minutes) in the background using PHP on a Windows machine.
source to share
This works great for a Windows machine, with the entire script output being written to $ response_file_path -
$command = $PHP_DIR." ../background_process/subscribe_bg.php -p=".$file_path_arg." >../sublogs/responses/".$file_response_path." 2>../sublogs/error_logs/err.txt";
execInBackground($command)
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
return 1;
}
else {
return 0;
//Or the code for linux machine.
}
}
source to share
I think you need to redirect the output from the background process to another file. Since you are not redirecting it, PHP is waiting for it. Add a '>' redirect to your command:
$command = "C:\wamp\bin\php\php5.5.12\php.exe ../background_process/subscribe_bg.php ".$file_temp_path . " > out.log ";
Good luck! Danilo
source to share