Page 1 of 1

Can I exit PHP while "exec" runs to avoid a script timeout?

Posted: Mon Jul 13, 2009 8:56 am
by daleyjem
Note:
I'm running PHP on IIS. Unless there's a drawback related to my question that comes as a result of using Windows/IIS, please spare me the "you should be on Linux/Apache" ridicule.

Anyway, here's an echo of an example exec or shell_exec string i'm passing:
C:\Inetpub\wwwroot\testing\video_upload_convert\bin\ffmpeg.exe -i "C:\Inetpub\wwwroot\testing\video_upload_convert\uploads\8.flv" -b 2000k -r 18 -ar 22050 -ab 128k "C:\Inetpub\wwwroot\testing\video_upload_convert\conversions\test.flv" 2> "C:\Inetpub\wwwroot\testing\video_upload_convert\queue\some_unique_id.txt"

Basically, I'm uploading a video file and then having ffmpeg convert it, whilst having some AJAX poll the text file for conversion progress. I've configured permissions on cmd.exe correctly and everything, so the conversion with ffmpeg IS working. My issue is with large video file conversions and the script timing out. These timeouts, however don't halt the conversion from continuing (kind of good news).

In regards to the documentation on this page:
http://us2.php.net/manual/en/function.exec.php
... specifically the part that says:
"Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends."

Is the "2>" method of outputting to a text file not considered a valid output stream? I'm not really sure why PHP needs to keep waiting for something when the process is now left to the server and ffmpeg.

Any suggestions?

Re: Can I exit PHP while "exec" runs to avoid a script timeout?

Posted: Mon Jul 13, 2009 9:18 am
by VladSun
From the users' comments:

Code: Select all

<?php 
function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
} 
?>
I would rewrite it as:

Code: Select all

<?php 
function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " 2>&1 > /dev/null &");   
    } 
} 
?>

Re: Can I exit PHP while "exec" runs to avoid a script timeout?

Posted: Mon Jul 13, 2009 9:25 am
by daleyjem
dude... looks like it's working precisely how i wanted! much thanks.