executing shell script from php - how?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

executing shell script from php - how?

Post by jito »

hi,
i am using shell_exec('pathtofile/myShellScript') to execute my shell script
but it fails to execute the script.

Any solution?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: executing shell script from php - how?

Post by volka »

jito wrote:but it fails to execute the script.
Do you get an error message?
If you do please post the error message.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you sure the path is correct? Are you sure the script is executable by php's user? Have you tried the other execution functions such as system()?
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

Post by jito »

100% sure the path is correct, even tried with the absolute path. i have tried passthru, exec other than shell_exec, but that also fails. And i'm getting no error message. Just going to check with system().
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Please also try

Code: Select all

<?php
$cmd = 'pathtofile/myShellScript';

echo 'is_file: ', is_file($cmd) ? 'true':'false', "<br />\n";
echo 'which: ', shell_exec('which '.$cmd);

$output = shell_exec($cmd);
echo htmlentities($output);
?>
see also: http://unixhelp.ed.ac.uk/CGI/man-cgi?which
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

Post by jito »

thank you volka, i already tried that. But i think problem is elsewhere. Actually i'm trying to create a new syatem user with the user and password generated from a php script. i think the problem is with the permission to craete user.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

What did it print?
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

Post by jito »

inside the shell script i ouput some values, and get tose values while running php script, so it's sure, the script is running without any problem. But the problem i think is with the shell script. I was trying to create a system user with the shell script.Normally i run that script and it worked, as i was the `root` user then, but when i'm trying to run that script using php it fails, as only root can create a user a/c. Is it clear?
Any help?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Please try

Code: Select all

<?php
$cmd = 'pathtofile/myShellScript';

echo 'sapi: ', php_sapi_name(), "<br />\n";
echo 'sapi: ', php_uname(), "<br />\n";
echo 'whoami: ', shell_exec('whoami'), "<br />\n";
echo 'is_file: ', is_file($cmd) ? 'true':'false', "<br />\n";
echo 'is_readable: ', is_readable($cmd) ? 'true':'false', "<br />\n";
echo 'is_executable: ', is_executable($cmd) ? 'true':'false', "<br />\n";

echo 'which: ', shell_exec('which '.$cmd.' 2>&1');
$output = shell_exec($cmd.' 2>&1');
echo htmlentities($output);
?>
and post the output.
If the output contains the dns name of your server and you don't want us to see it remove that part.
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

Post by jito »

sapi: apache2handler
sapi: *******************
whoami: apache
is_file: true
is_readable: true
is_executable: true
which: /usr/bin/newAcc.sh <H[JFile found The user id: hari Password is: ENujOT0S /usr/bin/newAcc.sh: line 51: //: is a directory Sorry, sudo >ust be setuid root. newusers: can't lock /etc/passwd. touch: creating `hari': Permission denied chown: `hari:mail': invalid user chmod: changing permissions of `hari': Operation not permitted /usr/bin/newAcc.sh: line 61: cd: /home/hari: No such file or directory touch: creating `.procmailrc': Permission denied chown: `hari:hari': invalid user /usr/bin/newAcc.sh: line 65: .procmailrc: Permission denied /usr/bin/newAcc.sh: line 66: .procmailrc: Permission denied /usr/bin/newA
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You can use sudo to grant the "user" apache root permissions for /usr/bin/newAcc.sh
Do not forget to set the NOPASSWD flag, otherwise sudo will ask for the account's password and the php process hangs.
Post Reply