cannot execute some commands

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
gamblor01
Forum Newbie
Posts: 2
Joined: Sun May 10, 2009 5:53 pm

cannot execute some commands

Post by gamblor01 »

Hi folks,

I'm trying to use shell_exec(), exec(), system(), or any function of that nature to execute a command, but it only seems to want to execute commands that are built-in to the Bash shell. For example, this works fine:

Code: Select all

 
$command = "cat /etc/passwd";
$myOutput = shell_exec($command);
echo "<pre>".$myOutput."</pre>";
 
When I visit my php page I see the contents of my /etc/passwd file. However, if I try calling a shell script or any other binary it fails. The commands are definitely in my PATH variable (not sure about the apache user's PATH) so I have tried fully-qualifying the commands like this:

Code: Select all

 
$command = "/usr/bin/java -version";
$myOutput = shell_exec($command);
echo "<pre>".$myOutput."</pre>";
 
Nothing prints when I attempt this. I even changed it to use system() instead of shell_exec() and checked the return code like this:

Code: Select all

 
$command = "/usr/bin/java -version";
$myOutput = system($command, $rc);
echo "$rc";
echo "<pre>".$myOutput."</pre>";
 
Doing so results in a return code of zero being printed to the browser, but the actual output of the command never returns anything like it should. So apparently it's executing the command but for some odd reason nothing get stored into $myOutput. The same thing happens if I attempt to invoke a shell script -- the return code is fine but the output just isn't there.

I have safe mode turned off, and safe_mode_exec_dir = /usr/local/php/bin. I tried putting scripts into this directory and executing them but that doesn't work either.

Am I missing something here? Why can't I invoke a script or other executable from my php code?
Last edited by Benjamin on Sun May 10, 2009 9:39 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: cannot execute some commands

Post by requinix »

Include stderr in the output and see if you got any error messages.

Code: Select all

/usr/bin/java -version  2>&1
gamblor01
Forum Newbie
Posts: 2
Joined: Sun May 10, 2009 5:53 pm

Re: cannot execute some commands

Post by gamblor01 »

No error messages but I do get this:
java version "1.5.0_16" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284) Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing) 0
Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing)
Nice! Thank you!!
Post Reply