exec doesn't work when called from standalone app

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
webfaqtory
Forum Newbie
Posts: 5
Joined: Fri May 29, 2009 8:44 am

exec doesn't work when called from standalone app

Post by webfaqtory »

Hi

exec (or popen etc) doesn't work when called from a standalone (commandline) PHP app but does if called from a browser (running under the web server) eg.

Code: Select all

<php
echo exec('ls');
?>
outputs nothing (even if su) if called from commandline eg php myapp.php. But it works as expected if called from a browser http;//myserver.com/myapp.php.

OS is Solaris 10. Is this a permissions problem? Can't see how if I'm su.

Thanks
Last edited by Benjamin on Fri May 29, 2009 10:36 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: exec doesn't work when called from standalone app

Post by jayshields »

Probably permissions based yeah.

One thing, why would you want to call a PHP script that calls exec() through a command-line prompt?
webfaqtory
Forum Newbie
Posts: 5
Joined: Fri May 29, 2009 8:44 am

Re: exec doesn't work when called from standalone app

Post by webfaqtory »

Its a twitter service that can take 10 minutes get the latest tweets, DMs, local news, weather etc. Its a an async call so the main server continues running whilst gathering the above data eg 'php getAsyncdata.php > /dev/null &'
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: exec doesn't work when called from standalone app

Post by VladSun »

Try

Code: Select all

<php
exec('ls 2>&1', $output);
print_r($output);
?>
and see what error messages you have.

Also, while 'php getAsyncdata.php > /dev/null &' is OK, I think you will find pcntl_fork() interesting :)
There are 10 types of people in this world, those who understand binary and those who don't
webfaqtory
Forum Newbie
Posts: 5
Joined: Fri May 29, 2009 8:44 am

Re: exec doesn't work when called from standalone app

Post by webfaqtory »

Hi VladSun

Thanks for your help.

Still the same, no output (other than the empty Array()). Called from a browser, of course, gives the full listing
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: exec doesn't work when called from standalone app

Post by VladSun »

What about

Code: Select all

exec('ls / 2>&1', $output);
print_r($output);
 
echo `ls / 2>&1`;
echo shell_exec('ls / 2>&1');
There are 10 types of people in this world, those who understand binary and those who don't
webfaqtory
Forum Newbie
Posts: 5
Joined: Fri May 29, 2009 8:44 am

Re: exec doesn't work when called from standalone app

Post by webfaqtory »

Hi VladSun

No joy, nothing is outputed for any command
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: exec doesn't work when called from standalone app

Post by VladSun »

Do you run it with:

Code: Select all

php getAsyncdata.php > /dev/null &
?
If so, try to run it like this:

Code: Select all

php getAsyncdata.php
There are 10 types of people in this world, those who understand binary and those who don't
webfaqtory
Forum Newbie
Posts: 5
Joined: Fri May 29, 2009 8:44 am

Re: exec doesn't work when called from standalone app

Post by webfaqtory »

2nd option 'php getAsyncData.php'
Post Reply