Run a script on a computer from php

人走茶凉 提交于 2021-02-10 06:38:47

问题


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


回答1:


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.




回答2:


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

/usr/bin/xdotool key XF86AudioPlay;


来源:https://stackoverflow.com/questions/9057076/run-a-script-on-a-computer-from-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!