简体   繁体   中英

shell_exec returns empty string

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); 

Most of the time php scripts are run by Apache, if thats the case with your script then Apache user may not have enough permissions to run this command. Check that first. If its run as CLI script then check if PHP user can run the script.

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

You may want to try " > file.txt 2>&1" at the end of your command. It will redirect the outputs to a separate file.

$command = "cmd command > outputs.txt 2>&1";
shell_execute($command);

You'll end up with a file that looks something like this:

b'cmd command output contents\r\n'

Maybe this is why that

Try to use system

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM