r/PHPhelp • • 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 .

8 Upvotes

15 comments sorted by

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?

2

u/Demon-Souls May 27 '22

You do know what it does right?

That line is warped around double quotation, yes I want the process to run in the background, I didn't need any output at all, and want the process runs even after I close the page, the script will show me the new created converted video file on the page.

1

u/horror-pangolin-123 May 28 '22

Did you try to run it from PHP without the ampersand?

As far as I know, shell terminates as soon as you disconnect from it, so it may be that since PHP is not there waiting for the output, shell just considers itself terminated and does nothing. Try it out without the ampersand and see what happens.

Or, add nohup in front of it. That means it's not going to terminate after shell disconnects.

https://linux.101hacks.com/unix/nohup-command/

2

u/Demon-Souls May 28 '22

Try it out without the ampersand and see what happens.

I have this code 2>/dev/null >/dev/null & I add it at the end, in every shell command I run from PHP, and it runs very well, I solve the issue, see updated OP.

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_exec can't send pipes to other process.

I run ffmpeg converting before this new command and it's works fine, I'm on folder owned by www-data user 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

u/[deleted] 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 with echo shell_exec ('ls /var/www/afolder |grep function.php'); it's retuned to me expected result .

1

u/[deleted] 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

u/[deleted] 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 Lightpd to my IP region and browser user agent

1

u/[deleted] 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

u/[deleted] 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 ) ;