exec() not working

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
Cepheus
Forum Newbie
Posts: 4
Joined: Mon Jun 14, 2010 6:15 am

exec() not working

Post by Cepheus »

Hi,

Im trying to run the exec() function using the code:

Code: Select all

<?php
	
	exec("whoami", $output);
	print_r($output);
?>
This returns a blank array, and in the apache error logs show: "sh: /whoami: No such file or directory". Safe mode is off.

whoami works fine in the console, as does any other applicatons i try to run (i.e. ffmpeg), but both fail using exec().

If anyone has any ideas on how to get this working it would be much appreciated.

Thanks in advance.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: exec() not working

Post by andyhoneycutt »

Seems to work fine on my system. Have you tried the full path to the binary? Perhaps:

Code: Select all

exec('/usr/bin/whoami',$output);
print_r($output); 
Cepheus
Forum Newbie
Posts: 4
Joined: Mon Jun 14, 2010 6:15 am

Re: exec() not working

Post by Cepheus »

Still no joy I’m afraid using the full path. I've checked the binaries permissions and they all seem ok. Apart from safe mode, is there anything else and needs to be specifically enabled/disabled?
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: exec() not working

Post by andyhoneycutt »

if you add the return code parameter to the exec() function, what result do you get out of it?

Code: Select all

exec("whoami", $output, $code);
print_r($output);
print_r($code); 
Cepheus
Forum Newbie
Posts: 4
Joined: Mon Jun 14, 2010 6:15 am

Re: exec() not working

Post by Cepheus »

It's returning "127" as a result.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: exec() not working

Post by VladSun »

Try

Code: Select all

exec("whoami 2>&1", $output, $code);
print_r($output);
print_r($code); 
There are 10 types of people in this world, those who understand binary and those who don't
Cepheus
Forum Newbie
Posts: 4
Joined: Mon Jun 14, 2010 6:15 am

Re: exec() not working

Post by Cepheus »

That got it working, thanks!
Post Reply