3

I'm unsuccessfully trying to execute a shell command from php. The goal is to switch on/off my music player of my computer/server via internet (with my phone for example). Here is what I would be able to do :

I have a very simple file "play.sh" : Code:

xdotool key XF86AudioPlay;
echo "switched";

if I run it ./play.sh, that works (music player switches on/off)

then I have an other very simple php file "play.php" : Code:

<?php echo shell_exec("./play.sh"); ?>

These two files are in the main folder of my server which is a partition of my computer. (I'm using lampp) But music is playing from my computer.

But when I'm going on localhost/play.php, I can see "switched" that showed me the sh file as been executed, but the sound doesn't turn off .

I just tried exec(), shell_exec(), passthru(), system(), .. with ./play.sh and /opt/lampp/.../play.php the result is exactly the same, and there is no error message.. :/

But that's weird, I'm not sure, but I think that what I run on my computer side is not the same than what I run on my server side. I mean that it's like the sound was turning on/off on the virtual server but had no link with the sound of my computer. That could be an explanation.. but then, how to execute a script on my computer from internet/my server..?

Does someone have an idea to do that ? Is it a configuration of apache..?

Thanks !

EDIT : Here is how I solved my problem :

xhost + local:www-data

Thanks for all your replies

Paulair
  • 41
  • 6
  • `xdotool` sends commands to *an* X server. Your desktop is probably running on display `:0` - try `export DISPLAY=:0; xdotool key XF86AudioPlay`; also, you may need to run your script as yourself - the PHP code might be running as user `www-data` or somesuch (just a few of the things that might be wrong with the script) – Piskvor left the building Jan 29 '12 at 21:48
  • also try using absolute path to xdtool, it may not be in apache PATH – dev-null-dweller Jan 29 '12 at 21:49
  • Add `2>&1` to get an error message. To grant X permissions use `xhost +localhost` as last resort. – mario Jan 29 '12 at 21:51
  • nope, I tried with export DISPLAY=:0; usr/bin/xdotool key XF86AudioPlay; but the result is still the same, and I granted X permissions to localhost but sill not working :/ – Paulair Jan 29 '12 at 22:14
  • here is the result of 2>&1 : No protocol specified Error: Can't open display: :0 Failed creating new xdo instance switched! – Paulair Jan 29 '12 at 22:18
  • you should mark Amber response as the one that was correct in this case – Elzo Valugi Jan 30 '12 at 08:31

2 Answers2

2

It may be a permissions issue - keep in mind that when PHP runs a command, it's run as the user the webserver is running as, e.g. www-data or apache or whatever. If your music player is running as your own personal user, your script may not have the ability to change it when run as a different user.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • Yes, I thought about that, so I put this is my /etc/sudoers : www-data ALL=NOPASSWD: /sbin/iptables, /usr/bin/du, /usr/bin/xdotool paul ALL=NOPASSWD: /sbin/iptables, /usr/bin/du, /usr/bin/xdotool apache ALL=NOPASSWD: /sbin/iptables, /usr/bin/du, /usr/bin/xdotool And the result is the same.. :/ – Paulair Jan 29 '12 at 22:01
0

Shell_exec works. Try to put in your .sh script absolute path to the executables

/usr/bin/xdotool key XF86AudioPlay;
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114