I am wanting to use ffmpeg to convert video to .flv in php. Currently I have this working, but it hangs the browser until the file is uploaded and is finished. I have been looking at the php docs on how to run an exec() process in the background, while updating the process using the returned PID. Here is what I found:
//Run linux command in background and return the PID created by the OS
function run_in_background($Command, $Priority = 0)
{
if($Priority)
$PID = shell_exec("nohup nice -n $Priority $Command > /dev/null & echo $!");
else
$PID = shell_exec("nohup $Command > /dev/null & echo $!");
return($PID);
}
There is also a trick which I use to track if the background task is running using the returned PID :
//Verifies if a process is running in linux
function is_process_running($PID)
{
exec("ps $PID", $ProcessState);
return(count($ProcessState) >= 2);
}
Am I suppose to create a separate .php file which then runs from the php cli to execute one of these functions? I just need a little nudge in getting this working and then I can take it from there.
Thanks!
You don't need to write a separate php script to do this (Though you may want to later if you implement some sort of queuing system).
You're almost there. The only problem is, the shell_exec() call blocks to wait for the return of the shell. You can avoid this if you redirect all output from the command in the shell to wither a file or /dev/null and background the task (with the & operator). So your code would become:
I don't think there is any way to retrieve the PID, unfortunately.
This is probably the way I would do it :
Here's a couple of other thoughts :
Your "processing script" would have to be launch every couple of minutes ; you can use cron for that, if you are on a Linux-like machine.
Edit : a bit more informations, after seeing the comment
As the processing part is done from CLI, and not from Apache, you don't need anykind of "background" manipulations : you can just use
shell_exec
, which will return the whole ouput of the command to your PHP script when it's finished doing it's job.For the user watching the web page saying "processing", it will seem like background processing ; and, in a way, it'll be, as the processing will be done by another processus (maybe even on another machine).
But, for you, it'll be much simpler :
Your processing script could look like something like this, I suppose :
Really easier than what you first thought, isn't it ? ;-)
Just don't worry about the background stuff anymore : only thing important is that the processing script is launched regularly, from crontab.
Hope this helps :-)