Execute shell command in PHP on the desktop of another logged in user
The Setup: I have a LAMP server (Ubuntu 9.10) which is also hooked up to my HDTV. Upon boot, one user is automatically logged in so as to have a desktop environment on screen.
What I want: I've been developing a 开发者_开发技巧remote, web interface in PHP for various things (play music, for example). However, I've hit a snag in that I would like to run a windowed program and have it display on the TV. Obviously, since PHP/Apache is running under the user www-data, this isn't going to happen just by running my command via exec().
Is there a Linux command that can run it as the currently logged in session of my other user, or a program that?
The (X) program starting uses the DISPLAY variable to determine which X session to hook up to. You'll need to figure out which X session the user currently has, if this is a one-user box is's most likely to be :0.
Then you could write a simple bash script to 1. Set the DISPLAY (and other variables as needed) 2. Execute.
--
Another solution would be to write the necessary information to a flat file and then have a cron job checking for updates every one or three seconds. The cron can be configured to run as a specified user. Ugly, but sould work.
As mataisf says - you need to set the DISPLAY variable so the program knows where to generate the window. However there is an authentication system which prevents unauthorized programs from access an X server (the place where the keyboard, screen and mouse are). One way around this is to let any program connecting from the local machine have access:
xhost +localhost
....but a better solution would be to run the program as the user logged in. There are lots of different ways to do this - but probably the most practical is via sudo, e.g.
sudo -u console_user program
Note that before you do this you might want to set the HOME variable so that xauth works properly (you can do this with the -H flag for sudo).
Note that you need to configure the program, the console_user and the webserver user in the /etc/sudoers file.
C.
精彩评论