Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shell_exec returns empty string

Tags:

php

shell-exec

When I execute my command in PHP with shell_exec it always returns an empty string. I tried shell_exec('ls -l'); and it works. I put the command as a separate file and still same result.

$shellOutput = shell_exec("pacmd list-sinks | grep 'volume: 0:' | tail -1 | awk '{ print $3 }'");

//return execute status;
echo trim($shellOutput); 
like image 550
Risto Novik Avatar asked Nov 14 '25 13:11

Risto Novik


1 Answers

I changed the sudoers to ALL ALL = (ALL) :NOPASSWD ALL (very un-secure, but just to find something sure to work),

then I did a

sudo -u myusername /path/to/script.sh

where script.sh has export VAR=value export VAR=value

for all the environmental variables that are necessary (you can do a printenv from a user who can properly pacmd to see what you need. probably a good HOME and maybe an XAUTHORITY).

hope that helps

like image 105
cyrusv Avatar answered Nov 17 '25 09:11

cyrusv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!