1

purpose: use php to input commands directly into the minecraft server console

Im trying to use a php script (run from browser) to exec() a shell script. when i run the php from a terminal it works! But in the browser, nothing happens.

exec('sudo -u root sh /home/minecraft/whitelist-reload.sh', $out, $ret_val);

When running from terminal, i get a "array 0" but the browser gives me a "array 1"

what is the issue? and once i run the shell, shouldn't everything after that work as if you were on a terminal?(does it matter what is inside of shell script?)

the shell has all rx permissions and is in the sudoers file as

www-data ALL = NOPASSWD: /home/minecraft/whitelist-reload.sh
ProjectPaatt
  • 15
  • 2
  • 8
  • make `/home/minecraft/whitelist-reload.sh` executable and try `'sudo -u root /home/minecraft/whitelist-reload.sh'` – MarcDefiant Aug 03 '12 at 11:31
  • Use shell_exec() instead for testing purposes. It will not give you the return value of the command but its output so you can possibly see what's going wrong. – inVader Aug 03 '12 at 11:42

2 Answers2

2

The problem is, that you run the script on a terminal as a user that probably has the sudo rights, whereas the apache/webserver user doesn't, so the $ret_val (which is actually just a status code) is set to 1 (means error).

try var_dump($out); in both cases to see the results of your exec call. To do this kind of thing from the browser, you might want to look into proc_open and family, or have a script that is chmod'ed to 777, so the apache user can run it, too. Let that script then call the actual shell script and return it's output back. This is, however very dangerous, and should only be used for testing environments on your own machine. Never do this in production environments!

I have posted a couple of questions here, too that might prove informative:

interaction over ssh

opening a second shell, and load profile variables AND call another script

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
0

Turns out... after inputting www-data into the sudoers file, all i needed to do was take of the "-u root" after it

ProjectPaatt
  • 15
  • 2
  • 8