LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to run a command remotely (https://www.linuxquestions.org/questions/linux-general-1/how-to-run-a-command-remotely-503081/)

fw12 11-19-2006 04:43 PM

How to run a command remotely
 
I can run this command successfully over ssh.

#/usr/bin/mplayer [options]

I want to be able to do the same using PHP.

But doing the following inside a php script does not work:

exec("/usr/bin/mplayer [options]");

I know the php's exec() works, since the following worked when I tried it:

exec("echo '/usr/bin/mplayer [options]' > /tvoutput");

So, I concluded that it must be that apache, which my web server runs as must not have permission to run the command /usr/bin/mplayer.

I then added apache to sudoers file.

But then this still doesn't work:

exec("sudo /usr/bin/mplayer [options]");

If I can issue
/usr/bin/mplayer [options]
from an ssh terminal and get the desired effect, I'm just baffled as to why I can't do it via a PHP script.

This is obviously not a PHP issue, since the command is being handed to linux correctly.

Any ideas? Sorry about the long explanation.

Thx.

macemoneta 11-19-2006 05:02 PM

This sounds like SELinux saving you from yourself. To verify check your /var/log/messages or /var/log/audit/audit.log for AVC denied messages. You can also temporarily disable SELinux to test with (as root):

setenforce 0

If it works, then you can re-enable SELinux with:

setenforce 1

You'll either need to create a local policy to enable execution, or check the booleans under menu System->Administration->Security Level and Firewall on the SELinux tab, HTTPD Service. One of the booleans may allow you to do what you are trying to accomplish.

Tralce 11-19-2006 05:10 PM

I'm having EXACTLY the same issue running a shell script from PHP, because the shell script needs to modify some of root's files.

fw12 11-19-2006 05:47 PM

Quote:

Originally Posted by macemoneta
This sounds like SELinux saving you from yourself. To verify check your /var/log/messages or /var/log/audit/audit.log for AVC denied messages. You can also temporarily disable SELinux to test with (as root):

setenforce 0

If it works, then you can re-enable SELinux with:

setenforce 1

You'll either need to create a local policy to enable execution, or check the booleans under menu System->Administration->Security Level and Firewall on the SELinux tab, HTTPD Service. One of the booleans may allow you to do what you are trying to accomplish.


# setenforce 0
setenforce: SELinux is disabled

fw12 11-19-2006 05:53 PM

Quote:

Originally Posted by Tralce
I'm having EXACTLY the same issue running a shell script from PHP, because the shell script needs to modify some of root's files.

You may look into modifying the sudoers file to allow a particular user on your system to temporarily run as root.

I think it goes something like this:

your_non_root_user ALL=(root) NOPASSWD: /command/to/run

Although people say it's not advisable to allow a user to run as root, but you can restrict the permission to just a single command as shown above.

macemoneta 11-19-2006 05:56 PM

Quote:

# setenforce 0
setenforce: SELinux is disabled
OK, now that you have disabled SELinux, test your code.

fw12 11-19-2006 07:41 PM

It wasn't enabled before. That's why I posted the ouput of the command.

macemoneta 11-19-2006 09:06 PM

Sorry, I was distracted. I see that SELinux is disabled.

fw12 11-20-2006 02:23 PM

Quote:

Originally Posted by macemoneta
Sorry, I was distracted. I see that SELinux is disabled.

Thank you for trying.
If you have other ideas, please share.

Is there a way to know if an error message is returned?

tail /var/log/messages shows nothing related to it.

macemoneta 11-20-2006 02:40 PM

Well, if you are running through your web browser, the error logs in /var/log/httpd/ should indicate any errors that were encountered.

fw12 11-20-2006 06:12 PM

You're correct. /var/log/httpd/error_log has this

sudo: sorry, you must have a tty to run sudo

After a little searching online, I found out FC6 has added more restrictions to sudo:

Defaults requiretty

I commented that out, and am no longer getting any errors in error_log, although my command still won't run.

I'm one step closer, thanks to you, but not quite there yet.

macemoneta 11-20-2006 06:26 PM

The next question I would have is, are you specifying the display (apache doesn't have one)? For example, if you are sending the video to your TV, the second head on your video card:

...mplayer -display :0.1 ...

fw12 11-21-2006 02:18 AM

Quote:

Originally Posted by macemoneta
The next question I would have is, are you specifying the display (apache doesn't have one)? For example, if you are sending the video to your TV, the second head on your video card:

...mplayer -display :0.1 ...

I've been using:
DISPLAY=:0.1 /usr/bin/mplayer

instead of your suggestion of:

/usr/bin/mplayer -dispaly :0.1

Anyway, after brooding over a few more hurdles, I finally go it to do what I wanted.

You've been a tremendous help. Not being formally trained in linux is a challenge for me. But with guys like you, and my resilience, it often works out in the end.


All times are GMT -5. The time now is 07:09 PM.