exec() command doesn't seem to work

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
TheStoryTeller
Forum Newbie
Posts: 15
Joined: Fri May 01, 2009 10:13 am

exec() command doesn't seem to work

Post by TheStoryTeller »

Hi, I'm trying to use the exec() command for a background computation. something like this:

Code: Select all

exec(" ./child.php /dev/null &");
It doesn't work.
I then found out that even simple use of exec() is not working.
I tried this:

Code: Select all

echo exec(`whoami`);
and also got blank, but when I do this:

Code: Select all

echo `whoami`;
I get the right answer on screen.
what am I missing here?
Last edited by Benjamin on Mon May 18, 2009 6:30 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: exec() command doesn't seem to work

Post by Benjamin »

Is php in safe mode? Are you getting any errors?
Note: When safe mode is enabled, you can only execute files within the safe_mode_exec_dir. For practical reasons, it is currently not allowed to have .. components in the path to the executable.
See: http://us.php.net/manual/en/function.exec.php
TheStoryTeller
Forum Newbie
Posts: 15
Joined: Fri May 01, 2009 10:13 am

Re: exec() command doesn't seem to work

Post by TheStoryTeller »

I guess I forgot to mention.. I checked the phpinfo() and safe mode is turned off.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: exec() command doesn't seem to work

Post by Benjamin »

Try this:

Code: Select all

 
echo "exec returned: " . exec('php -r ./child.php > /dev/null', $result, $rcode]) . '<br />';
echo '<pre>' . print_r($result, true) . '</pre>';
echo "Return Code: $rcode<br />";
 
TheStoryTeller
Forum Newbie
Posts: 15
Joined: Fri May 01, 2009 10:13 am

Re: exec() command doesn't seem to work

Post by TheStoryTeller »

astions, I tries your code and again nothing happens. there's no error message - just nothing is printed.
edited: I was mistaken, something does appear.
Last edited by TheStoryTeller on Tue May 19, 2009 2:56 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: exec() command doesn't seem to work

Post by Benjamin »

Copy and paste the output.
TheStoryTeller
Forum Newbie
Posts: 15
Joined: Fri May 01, 2009 10:13 am

Re: exec() command doesn't seem to work

Post by TheStoryTeller »

astions wrote:Copy and paste the output.
ok, my mistake: something is written:

Code: Select all

exec returned:
 
Array
(
)
 
Return Code: 254
what can you make of this?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: exec() command doesn't seem to work

Post by Darhazer »

The difference between exec() / system() and the ` operator is that the functions return only the last line of the output, and if it is a empty string, or if output is piped to /dev/null, you won't receive anything.
` operator, like the passthru function, return the whole output.
TheStoryTeller
Forum Newbie
Posts: 15
Joined: Fri May 01, 2009 10:13 am

Re: exec() command doesn't seem to work

Post by TheStoryTeller »

Darhazer wrote:The difference between exec() / system() and the ` operator is that the functions return only the last line of the output, and if it is a empty string, or if output is piped to /dev/null, you won't receive anything.
` operator, like the passthru function, return the whole output.
If that is so why does the code

Code: Select all

echo exec(`whoami`);
return nothing?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: exec() command doesn't seem to work

Post by Darhazer »

Try using passthru and see the results, maybe your server outputs a empty line at the end. On my hosting, both passthru('whoami') and echo exec('whoami') output my username.
Post Reply