Walter Schrabmair
Walter Schrabmair

Reputation: 1341

How can I redirect the output of a exec call in php ( and its stderr) to the calling program?

I have find this solution but it is not 100% ok for me. output of child-prc

I have following php file:

    <?php
    printf("$argv[1]: ". date("H:m:s")."\n");
    if ($argv[1]=='1') {exec('/usr/bin/php -f t.php 2 2&>1');}
    ?>

Now I do a call on the prompt and want to redirect all output (also from the exec) to on log file.

php -f t.php 1 > t.log

The result should be:

    1: 15:10:52
    2: 15:10:53

the line

    2: 15:10:53 

is from the exec child process.

But I get only line one.

Please notify that the name of the log file t.log should be not hard-wired defined.

What make I wrong? THANKS

Upvotes: 0

Views: 1192

Answers (1)

Stephane Chazelas
Stephane Chazelas

Reputation: 6239

PHP's exec() is quite unlike the exec() of any other language.

When invoked as just exec("some string"), it gets some string interpreted as shell code by a shell in a child process, and returns the last line of its output stripped of all the trailing ASCII whitespace characters.

Here, you're not doing anything with that output though.

Now as to that:

/usr/bin/php -f t.php 2 2&>1

shell code, note that the behaviour varies between sh implementations.

In POSIX compliant sh implementations, that's meant to be the same as:

/usr/bin/php -f t.php 2 2 &
>1

That is run that php command in background, and then open the file called 1 for writing, but without any command whose output to send there.

Not all sh implementations are compliant there are some shells implement a non-standard &> redirection operator.

In the GNU implementation of sh (bash), that's interpreted as:

/bin/bin/php -f t.php 2 2 &> "1"

Which is running that php command (with 2 "2" arguments) with both stdout and stderr redirected to the file called 1. So it will produce no output on stdout (captured by php) but send /usr/bin/php's output to that file.

In zsh, the 2&> "1" redirects both fd 2 and fd 2 to the file called 1, as if you had written 2> "1" 2>&2. With fd 2 redirected twice, zsh normally implements a tee-like behaviour where the data is sent to both locations (here the same), but when invoked as sh (not that many systems have sh implemented with zsh), zsh disables that tee-like feature, so the 2&>1 ends up being the same as 2> "1", so redirects stderr to the file called 1. In any case, since that php command doesn't output anything on stderr, it makes no difference.

mksh (MirBSD sh) behaves like zsh-as-sh above.

Here, you could do

echo exec('/usr/bin/php -f t.php') . "\n";

To print the contents of the last line of the output of that php command followed by a newline character. Or (maybe what you intended?):

echo exec('/usr/bin/php -f t.php 2>&1') . "\n";

For the last line of the output+errors of that php command.

But here, it sounds like you just want to leave the output of that php command untouched; not capture it, but let it go to the same destination as the echo does.

PHP has some system() and passthru() functions, but those capture the output of the shell command to display it afterwards.

You can however use the pcntl_exec() function which is more like the exec() of other languages, except that it doesn't let you set argv[0].

pcntl_exec("/usr/bin/php", ["-f", "t.php", "2"]);

Like the exec() of other languages, it runs it in the same process, so it's the last thing the php script will do. And it doesn't invoke a shell, just runs the command directly.

That's probably not what you'll want to use in a web application though. In a web application, php needs to know what output it's going to send before it sends it, if only to fill in Content-Length type of HTTP header. There, you'd use passthru() or system() or shell_exec()...

See also:

Upvotes: 2

Related Questions