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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
daleyjem
Forum Newbie
Posts: 4
Joined: Sun Mar 09, 2008 11:32 am

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

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post 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 &");   
    } 
} 
?>
There are 10 types of people in this world, those who understand binary and those who don't
daleyjem
Forum Newbie
Posts: 4
Joined: Sun Mar 09, 2008 11:32 am

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

Post by daleyjem »

dude... looks like it's working precisely how i wanted! much thanks.
Post Reply