View Full Version : how to execute a bash file from php ?

01-06-2006, 08:08 AM

i have a bahs file test in /usr/sbin/

command is :
su - root
password: rootpswd
/usr/bin/test start

if i run this command through command line as root login then i am able to run bash file its working fine i.e it starts a server . but when i use
exec('su - root');
exec('password: rootpswd' );
exec('/usr/sbin/test start');

in php script then i am not getting the output .
someone plz help me how to execute a bash file from php and how to show the output back to the page .

thanks in advance for the reply.

01-06-2006, 08:14 AM

It may not be a problem with PHP, but with the permissions system set up on the server. Remember that scripts processed by PHP are typically done so as "NOBODY". The following may be of use for you:


Best of luck!


ralph l mayo
01-06-2006, 09:15 AM
Putting 'nobody' in sudoers may make the script work but it's really not such a hot idea.

Velox Letum
01-06-2006, 10:00 AM
I haven't managed to get PHP to use su correctly yet. First, password: that appears on the screen is a prompt, not a command, so somehow you'd have to echo (the shell command) the password to the correct virtual terminal. You'd probably just have to pipe the output of echo password to /dev/ptysomething. I'll look into experimenting with it later.

01-06-2006, 10:19 AM
do i have to change the settings in php.ini to make it work

safe_mode_exec_dir = path to the script (/var/wwwhtml/rheaserv/)
safe_mode_include_dir = /usr/sbin/test
safe_mode_allowed_env_vars = /usr/sbin/test

when u run the ' /usr/sbin/test start ' it will start asterisk server

is there anything i have to make change in the php.ini or is it the problem of asterisk ?

Velox Letum
01-06-2006, 10:41 AM
If you want root access to a certain script, it looks like you'll just have to add a line to /etc/sudoers (using the visudo command) allowing the Apache user the carte blanche to run the command without a password, that way you can just run sudo /usr/sbin/test start and it'll run it as root without question. A bit insecure, but that's about as good as it gets. The include dir for safemode should be the directory, /usr/sbin/ not the actual file.

01-07-2006, 06:09 AM
Putting 'nobody' in sudoers may make the script work but it's really not such a hot idea.

Ralph l mayo is correct. Is there any way that you could have the script run without root privilages?

Or, and I haven't tested this, what about just setting the owner of the file to root, the sticky-bit on the file so that when it executes it will execute with the root permissions and add o+x permissions?