stdout redirect in exec function

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
tomolas
Forum Newbie
Posts: 5
Joined: Mon Jan 21, 2008 5:22 am
Contact:

stdout redirect in exec function

Post by tomolas »

Hi.
I have a little problem redirecting the output of program called from exec function in php.
Simple example:

Code: Select all

 
$output = shell_exec('ls -al &>my_file.txt');
 
ls -al will of course be replaced with something else later.
I know that the output this produces will be in $output BUT, i don't know why it's not in the my_file.txt file.
I need the output to be writen to a file while it's produced. I don't need it after to process has finished.
I will have some other program reading my_file.txt while it's being writen to by some other process.

If you try ls -al &> my_file.txt in your console, it will work. But not if you do it this way in php.
Do you have any idea why this doesn't work in php ?

Thank you!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: stdout redirect in exec function

Post by VladSun »

Try:

Code: Select all

$output = shell_exec('ls -al  2>&1 > my_file.txt');
and see what errors you get, if any.

Also, in general you don't need the & :

Code: Select all

$output = shell_exec('ls -al  > my_file.txt');
And last - use absolute paths.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply