I have an exec function in php file that execs a bash script. It script calls fmpeg to transcode a video file.
How can I know when transcoding is finish??
$script = "/opt/lamp../name.sh"
exec("$script $videoIn $id")
I will try using next code but it doesn't workd.
if (exec("$script $videoIn $id"))
{
//print on screen that the video has been transcoded
}
The function exec() will return when the executed command is finished. My guess is that the command fails somehow (possibly because you're not using escapeshellcmd() and escapeshellarg()).
Your php script waits for the exec'd command to be finished before going on.
exec does not return the command's return value.
string exec ( string $command [, array &$output [, int &$return_var ]] )
you have to provide a var where that value will be written.
Related
I am having difficulty processing bash shell in php.
My problem is as below.
I have 3 tasks corresponding to 3 files bash shell (task1.sh, task2.sh, task3.sh). When task1.sh finishes processing, task2.sh will automatically execute, when task2.sh finishes processing, task3.sh will automatically execute.
Initially, I wrote a file called task.sh and embedded task1.sh, task2.sh, task3.sh into it. But I want to embed these 3 tasks into a php file.
For example: I create task.php and do the following:
If task1.sh fails, it will display popout (alert) error message.
If task1.sh processing is complete then task2.sh will continue to be automatically done.
The processing of task2.sh and task3.sh is similar to the above.
All 3 tasks I want to run backgroud. The problem is that when I run the background bash shell, I will not be able to check the failed error statement (the result always returns to 0).
I have learned a lot and consulted many documents but it did not help me.
I hope you can support me.
Sorry, my english very poor.
You may use the $retval argument of exec().
<?php
exec('task1.sh', $output, $retval);
if ($retval !== 0) {
// task 1 failed
exit('Error running task1: ' . implode("<br/>\n", $output));
}
exec('task2.sh', $output, $retval);
if ($retval !== 0) {
// task 2 failed
exit('Error running task1: ' . implode("<br/>\n", $output));
}
exec('task3.sh', $output, $retval);
if ($retval !== 0) {
// task 3 failed
exit('Error running task1: ' . implode("<br/>\n", $output));
}
You can simply use the function:
exec ( string $command [, array &$output [, int &$return_var ]] ) : string
With:
output :
If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().
return_var :
If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.
Thus you could use $output and $result_var to check the execution errors of your shells.
Regards
I am unable to get php to show full results of my python script, only the last basic print statement is printed. What am I doing wrong here? Why is the php not showing the output from the python script?
php
$result = exec('python list.py');
echo $result;
list.py
import subprocess
print "start"
a = subprocess.Popen(["ls", "-a"], stdout=subprocess.PIPE)
a.wait()
result_str = a.stdout.read()
print result_str
print "stop"
the command line output for that python script is as below
start
...filename
...filename
...etc...etc
stop
The php output from executing the python script is only
stop
Thank you
You need to pass $result as a param into exec() otherwise you only get the last statement.
exec('python list.py', $result);
print_r($result);
This will get an array of output, $result[0] will contain stop and $result[sizeof($result)-1] will contain stop and everything in between will contain the rest of your program output.
exec will always return the last line from the result of the command. So, if you need to get every line of output from the command executed. Then, you need to write as below :
$result = exec('python list.py', $ouput);
print_r($output)
As per exec documentation
string exec ( string $command [, array &$output [, int &$return_var ]] )
If the output argument is present, then the specified array will be
filled with every line of output from the command. Trailing
whitespace, such as \n, is not included in this array. Note that if
the array already contains some elements, exec() will append to the
end of the array. If you do not want the function to append elements,
call unset() on the array before passing it to exec().
I am using a ffmpeg command for watermarking a video.
it does the work but i need to detect wheather it is executed successfully or not.
my command:
$mark = "ffmpeg -i ".$inputvideo." -i logo.png -filter_complex ". '"overlay=x=(main_w-overlay_w):y=(main_h-overlay_h)"'." ".uniqid()."html56.mp4";
For output i used something like:
$x = exec($mark);
print_r($x);
But i am not getting anything printed in place of $x.
After some searching I found this statement for exec command
string exec ( string $command [, array &$output [, int &$return_var ]] )
$s=exec($mark,$var);
$var is my return var.
Now when i print $var i am getting an empty array.
Please suggest where i am missing.
string exec ( string $command [, array &$output [, int &$return_var ]] )
When you do $s = exec($mark, $var); your $var corresponds to $output which is empty because ffmpeg outputs information to sderr since stdout might be used for actual data output.
If you want to get the return code all preceding optional arguments must be specified even if you don't use them:
$s = exec($mark, $output, $var)
If you need the actual output you can redirect the stderr to stdout since you're not using it by placing an 2>&1 at the end of your command or by using PHP's proc_open() to execute it.
I want to process my access log in php - checking some IPs, which are leeching content and so on, everything in PHP, running as CLI. I tried to make following, but it never pass exec tail -f, so, actually I can not process the data. Any help appreciated.
$log = '/var/log/lighttpd/web.org-access.log';
$pipefile = '/www/web.org/tmp/fifo.tmp';
if(file_exists($pipefile))
if(!unlink($pipefile))
die('unable to remove stale file');
umask(0);
if(!posix_mkfifo($pipefile,0777))
die('unable to create named pipe');
exec("tail -f $log > $pipefile 2>&1 &"); //I tried nohup and so on...
//exec("varnishncsa $log > $pipefile 2>&1 &"); //will be here instead tail -f
echo "never comes here"; //never shows up
If possible, I want to do it just in PHP, no bash/tcsh scripting (I know how to do it using those).
Thanks.
If you want exec to start a background process, you will have to redirect its output.
Quote from the manual:
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.
Notice the full syntax description:
string exec ( string $command [, array &$output [, int &$return_var ]] )
Source: http://www.php.net/manual/en/function.exec.php
I have a simple php exec command that calls svnlook. If I run the command through the terminal I get all the output I expect. If I run it as shown below, I only get the last item.
$list = exec("svnlook changed -r ".$urlCleaned." ".$SVNEXPORT);
echo $list;
Can I buffer the output? If so how? And will that help?
That's by design and is explained:
string exec ( string $command [, array &$output [, int &$return_var ]] )
Return Values
The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.
To get the output of the executed command, be sure to set and use the output parameter.
http://php.net/manual/en/function.exec.php
exec("svnlook changed -r ".$urlCleaned." ".$SVNEXPORT, $output);
var_dump($output);
Alternatively, shell_exec returns everything.