r/PHPhelp • u/Demon-Souls • May 27 '22
Solved php shell_exec not working with pipes
Hi all I'm trying to SvtAv1EncApp tools via web interface, I try to us both exec() shell_exec() with no success , BTW I print the command and run it into the shell directly and it's works perfectly fine.
$cmd2="/usr/local/bin/ffmpeg -loglevel -8 -i "$mp4" -s 960x540 -strict -1 -f yuv4mpegpipe - | /usr/local/bin/SvtAv1EncApp --no-progress -i stdin --rc 0 -q 38 --preset 8 -b stdout 2>/var/www/vl/ffmpeg.log | /usr/local/bin/ffmpeg -loglevel -8 -y -i - -i "$mp4" -map 0:v -map 1:a:0 -c:v copy $a '".$mpa."_.mkv' & ";
shell_exec( $cmd2 ) ;
Here is the text of echo $cmd2 output
/usr/local/bin/ffmpeg -loglevel -8 -i "FHD.mp4" -s 960x540 -strict -1 -f yuv4mpegpipe - | /usr/local/bin/SvtAv1EncApp --no-progress -i stdin --rc 0 -q 38 --preset 8 -b stdout 2>/var/www/vl/ffmpeg.log | /usr/local/bin/ffmpeg -loglevel -8 -y -i - -i "FHD.mp4" -map 0:v -map 1:a:0 -c:v copy -strict -2 -c:a libopus -b:a 64k 'FHD_.mkv' &
=Update=
I kinda solve it , I don't know why but exec() successfully executed the script , I created new bash script from command line thanks to @xisonc suggesting nano /usr/local/sbin/av1c with this value
!/bin/sh
touch /var/www/vl/ffmpeg.log /usr/local/bin/ffmpeg -loglevel -8 -i "$1" -s 960x540 -strict -1 -f yuv4mpegpipe -
| /usr/local/bin/SvtAv1EncApp --no-progress -i stdin --rc 0 -q 38 --preset 8 -b stdout 2>/var/www/vl/ffmpeg.log
| /usr/local/bin/ffmpeg -loglevel -8 -y -i - -i "$1" -map 0:v -map 1:a:0 -c:v copy -strict -2 -c:a libopus -b:a 64k "$2"
On the php script i had this
$cmd2="/usr/local/sbin/av1c '$mp4' '$mpa"."_.mkv' 2>/dev/null >/dev/null & " ;
exec( $cmd2 , $pid, $r ) ;
var_dump( $r ) ;var_dump returns 0 , and since the bash script are silent I don't need to see the output , now I want to change it to wok on the background it's running on background now after adding 2>/dev/null >/dev/null & at the end of the command .
1
u/MateusAzevedo May 27 '22
Use the exec() function instead and make sure to provide the $output and $result_code arguments.
Then, you'll be able to check the error to figure out the problem.
Remember that from the web context, the user running the command is "www-data" (if installed as Apache mod-php) or whatever user you have configured for PHP-fpm. So, this could be a simple permission issue.
1
u/Demon-Souls May 27 '22 edited May 27 '22
Use the exec() function instead and make sure to provide the $output and $result_code arguments
I did use exec() from the beginning, and no output accrued, + I don't want to see the output on the browser, the above command are silent, and should work on the background . Anyway the code work perfectly from command line, I suspected that both
exec() shell_execcan't sendpipesto other process.I run
ffmpegconverting before this new command and it's works fine, I'm on folder owned bywww-datauser and I can create delete files easy.2
u/MateusAzevedo May 27 '22
Sure you don't need the actual output, but the ideia is to debug the problem.
The only way to trace the error is to run the command as www-data user, with all output available, so if anything is wrong, you will know.
I did use exec() from the beginning, and no output accrued
I think you need to remove the & at the end too, so PHP will wait for everything to run, otherwise you won't have the output (I think).
Anyway, I'm just providing some ideas to help debug the problem.
Also, the symfony/process compoment may be helpful.
1
May 28 '22
I think this would make more sense to wrap your command into a shell script, then call the shell script in PHP instead.
Eg:
encode.sh
#!/bin/sh
/usr/local/bin/ffmpeg -loglevel -8 -i "FHD.mp4" -s 960x540 -strict -1 -f yuv4mpegpipe - | /usr/local/bin/SvtAv1EncApp --no-progress -i stdin --rc 0 -q 38 --preset 8 -b stdout 2>/var/www/vl/ffmpeg.log | /usr/local/bin/ffmpeg -loglevel -8 -y -i - -i "FHD.mp4" -map 0:v -map 1:a:0 -c:v copy -strict -2 -c:a libopus -b:a 64k 'FHD_.mkv'Then in PHP just run it:
shell_exec('/path/to/encode.sh &');1
u/Demon-Souls May 28 '22 edited May 28 '22
I did create the script and didn't work with
shell_exec() exec()it's returns NULL, I run it from the command line and it's works perfectly
/usr/local/sbin/av1c '30s.mp4' '30s_.mkv'BTW I did run
echo shell_exec ('ls /var/www/afolder');from the php script, and runs very well same withecho shell_exec ('ls /var/www/afolder |grep function.php');it's retuned to me expected result .1
May 28 '22
So you understand that if you append
&to any script you run via exec or shell_exec, it will never return a result, right?On Unix, appending
&to the end of the command will detach it from the shell and run it in the background. You will never receive the output if you append the ampersand symbol.1
u/Demon-Souls May 28 '22
So you understand that if you append
The script goal is not post any output, this is silent bash scrip/command, the issue with command I posted above were quite complicated, it contains three steps/commands , each one pipe it output to the next process, Anyway I solve it see the OP update.
1
May 28 '22
This function and any other accessing the shell from web page should be blacklisted in web pages. You have CLI for this types of operations running under secure non web user account.
Shell injection is real. Just my 2 cents on this.
Cheers 🥂.
2
u/Demon-Souls May 28 '22
This function and any other accessing the shell from web page should be blacklisted in web pages
That page is blacklisted from the web server
Lightpdto my IP region and browser user agent1
May 28 '22
Ok, just saying. I've experienced this and still am but now environment is secured as much as it is possible... Still, from BotNet networks I do receive attacks on a daily basis. Coming from all around the world, and devices as well... Some of them still do manage to break through all levels to the request. I see them in the logs and are mostly shell or code injection.
Stay well protected, take care and 🥂!
1
May 28 '22 edited May 28 '22
exec works as expected.
var_dump(
exec(
'/bin/bash -c "echo 123456789 | grep -o 5"',
$output,
$result_code
),
$output,
$result_code
);shell_exec works as expected.
var_dump(
shell_exec("echo 123456789 | grep -o 5")
);Did you forget to escape your quotes? Also, using ampersand to background a process is not going to work here...
$cmd2="/usr/local/bin/ffmpeg -loglevel -8 -i \"$mp4\" -s 960x540 -strict -1 -f yuv4mpegpipe - | /usr/local/bin/SvtAv1EncApp --no-progress -i stdin --rc 0 -q 38 --preset 8 -b stdout 2>/var/www/vl/ffmpeg.log | /usr/local/bin/ffmpeg -loglevel -8 -y -i - -i \"$mp4\" -map 0:v -map 1:a:0 -c:v copy $a '".$mpa."_.mkv' & ";
shell_exec( $cmd2 ) ;
2
u/Idontremember99 May 27 '22
Did you really mean to put the ampersand there at the end? You do know what it does right?