4

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); 
Risto Novik
  • 8,199
  • 9
  • 50
  • 66
  • As mentioned in the manual `exec()` returns the last line of the output of the executed command. You are sure, that your command produce any output, or that it not output an empty line at the end? – KingCrunch Jun 24 '11 at 13:21
  • well this is when i execute in command line no empty lines. risto@risto-desktop:/var/www/PulseAudioVolumeControl$ ./volume 24% – Risto Novik Jun 24 '11 at 13:23
  • try `shell_exec("which pacmd")` to see if your program is included in `$PATH` used by PHP – dev-null-dweller Jun 24 '11 at 13:33
  • 3
    Add `2>&1` after `pacmd list-sinks` or look into the error.log for messages. It's most commonly command not found. – mario Jun 24 '11 at 13:34
  • you meant this $shellOutput = ("pacmd list-sinks 2>&1 | grep 'volume: 0:' | tail -1 | awk '{ print $3 }'"); and no errors on this command, I tired to execute ll command and then in error.log erros listed but on pacmd no errors. sh: ll: not found – Risto Novik Jun 24 '11 at 16:17
  • and the shell_exec("which pacmd") returned correct path on web /usr/bin/pacmd – Risto Novik Jun 24 '11 at 16:18

4 Answers4

2

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

cyrusv
  • 247
  • 3
  • 15
  • 1
    Another thing you can try is running pulse with the --system tag. I haven't tried that yet, but it's on my to-do list. It makes it not specific to the logged in user. – cyrusv Nov 30 '11 at 21:52
  • Could you by any chance have an answer to [this](http://stackoverflow.com/questions/33974037/shell-exec-empty-response-for-nslookup-query)?? – Parthapratim Neog Nov 28 '15 at 17:20
1

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'
1

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.

Kumar
  • 5,038
  • 7
  • 39
  • 51
  • on php CLI the command runs exactly as needed. So the problem might be in web side settings on Apache ? – Risto Novik Jun 24 '11 at 16:46
  • This clears it, Linux has a user/group permission system, now you just need to check if Apache user has the permissions to run this command or not – Kumar Jun 24 '11 at 17:03
  • well yes this might be the problem but i do not have any clue how to fix this, i tried to make the script executable by adding group SGID 4711 and changed group user to root:root. No luck still, any alternatives ? – Risto Novik Jun 24 '11 at 17:52
  • You need to let *Apache* user run the command, on my Debian box Apache user is `www-data` so I will let `www-data` run `pacmd`, how to do this, add `www-data` to same user group which can currently run `pacmd`, add this `www-data ALL=(ALL) NOPASSWD:ALL` in `/etc/sudoers` and try to `su www-data` and then run `pacmd` – Kumar Jun 24 '11 at 18:11
0

Maybe this is why that

Try to use system

Community
  • 1
  • 1
Kaiser
  • 748
  • 7
  • 21