Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

redirect stdout and stderr to PHP variables?

5,250 views
Skip to first unread message

crankypuss

unread,
Jan 25, 2012, 2:47:11 PM1/25/12
to
I'm sure there's a way to do this, probably some simple syntax I've not
run into and am too stupid to find in the manual or through google-fu or
otherwise.

I'd like to issue an arbitrary shell command in a subroutine and have it
return an array that contains one element representing stdout and
another element representing stderr.

For example, using tar to deal with a bazillion files, error messages
might not be seen when specifying verbose output; I'd like to collect
them and display after stdout.

I've not found any syntax, yet, for redirecting to a PHP variable rather
than some file.

tia.

M. Strobel

unread,
Jan 25, 2012, 4:42:36 PM1/25/12
to
No problem here:

php > $res = shell_exec('rm this-does-really-not-exist.php 2>&1');
php > echo $res;
rm: Entfernen von „this-does-really-not-exist.php“ nicht möglich: Datei oder
Verzeichnis nicht gefunden
php >

Maybe you drop this requirement "array of...".

/Str.

M. Strobel

unread,
Jan 25, 2012, 4:46:43 PM1/25/12
to
Am 25.01.2012 20:47, schrieb crankypuss:
Or like that, if you are fond of arrays:

php > $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a, $rc);
php > echo $rc;
1
php > print_r($a);
Array
(
[0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht möglich: Datei
oder Verzeichnis nicht gefunden
)
php >

/Str.

crankypuss

unread,
Jan 25, 2012, 5:53:56 PM1/25/12
to
On 01/25/2012 02:46 PM, M. Strobel wrote:
> php> $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a, $rc);
> php> echo $rc;
> 1
> php> print_r($a);
> Array
> (
> [0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht möglich: Datei
> oder Verzeichnis nicht gefunden
> )
> php>

Thanks, will try these in the morning.

crankypuss

unread,
Jan 26, 2012, 3:33:03 AM1/26/12
to
BTW, where is this syntax documented? I'd prefer understanding it to
using magic.

M. Strobel

unread,
Jan 26, 2012, 3:46:42 AM1/26/12
to
Hey, I just booted my system in the morning, and checked for news...

see here: http://www.gnu.org/software/bash/manual/bashref.html#Redirections

The windows cmd.exe works quite the same in this point.

/Str.

Message has been deleted

crankypuss

unread,
Jan 26, 2012, 4:23:07 AM1/26/12
to
Sorry, I might be more dense than I realized, but what does bash have to
do with php here? I might not (and often will not) be running a cli
script but rather running commands as part of generating an html page
for a web server.

crankypuss

unread,
Jan 26, 2012, 4:27:35 AM1/26/12
to
On 01/26/2012 02:00 AM, Tim Streater wrote:
> In article <jfr34...@news4.newsguy.com>,
> Which syntax, exactly? exec is documented in the PHP online docs.

Sorry, I've been unable to find the bit about redirecting stdout and
stderr to PHP variables, can you point me to that? Yes, exec() provides
an optional &$output argument which in effect redirects stdout to the
varialble, but I see no way to obtain stderr separately. Thanks.

Erwin Moller

unread,
Jan 26, 2012, 5:43:38 AM1/26/12
to
Hi crankypuss,

You only see this redirecting in functions that handle with STDERR and
STDOUT, functions like exec() you already use.
When you are working within 'normal' PHP (as in "No external processes
invoked by user", so no exec() and the like) you don't have the concept
of STDERR directly, nor do you need it.
Of course: errors can still occur, but PHP will handle them. You can
modify HOW you want PHP to handle them (logging into file/errorlog, put
them on screen, invoke some custom-errorhandler/etc).

To put it very simple (and probably incorrect) to get the general idea:
You can think of PHP in relation with STDIN and STDOUT and STDERR like this:

STDIN for PHP is what is feeded to PHP (via Apache, or commandline)
So that consists typically of things like (part of) URL, cookies,
referrer, etc.

STDOUT is what PHP produces and is returned to the client (most of the
time with a webserver inbetween).

STDERR is where PHP directs its erorrs to. (Think logfile, and/or to
STDOUT: this is all configurable.)

But you don't see STDIN/STDOUT/STDERR when you work with PHP without
using exec().

Regards,
Erwin Moller

--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens

crankypuss

unread,
Jan 26, 2012, 6:14:34 AM1/26/12
to
So how do you capture only STDERR when using exec() ?

M. Strobel

unread,
Jan 26, 2012, 7:25:41 AM1/26/12
to
You can not capture only stderr, because exec returns only stdout. So you only can
redirect stderr to stdout.

You can do it with http://es.php.net/manual/en/function.proc-open.php

Maybe you should explain what your basic problem is. Mentioning the environment so we
do not have to guess.

/Str.

Michael Fesser

unread,
Jan 26, 2012, 8:35:43 AM1/26/12
to
.oO(crankypuss)

>Sorry, I might be more dense than I realized, but what does bash have to
>do with php here?

Running exec() and the like invokes a shell, which then executes the
given command. Redirecting STDERR ist part of that shell, not PHP.

Micha

--
http://mfesser.de/blickwinkel

crankypuss

unread,
Jan 26, 2012, 9:11:15 AM1/26/12
to
>>>> an optional&$output argument which in effect redirects stdout to the
As far as I'm concerned that's pretty useless.

> You can do it with http://es.php.net/manual/en/function.proc-open.php
>Maybe you should explain what your basic problem is.

I have. Lots of stdout lines with a few stderr lines, I want to output
the stderr lines *after* stdout instead of interspersed.

Erwin Moller

unread,
Jan 26, 2012, 10:18:20 AM1/26/12
to
Hi Crankypuss,

Read this:
http://nl.php.net/manual/en/function.proc-open.php

It is a little more complicated, but you can capture what you need
(STDERR in this case).

Very off topic:
Concerning your wish to intergrate PHP with a windowing environment, I
found this:

http://en.wikipedia.org/wiki/PHP-Qt

I haven't used it myself, only with C++ so I have no clue how good it
all is, but QT is solid.

Jerry Stuckle

unread,
Jan 25, 2012, 5:25:50 PM1/25/12
to
Take a look at the proc_open(). The examples show how do return stdout
and stderr in separate streams.

It may be more than you need (this is made to be interactive between the
PHP script and the process), but you don't HAVE to send data to the process.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

Peter H. Coffin

unread,
Jan 25, 2012, 5:52:35 PM1/25/12
to
Sorry, you have to use the file, then read the file if you want to parse
back in what went out on stderr.

--
85. I will not use any plan in which the final step is horribly
complicated, e.g. "Align the 12 Stones of Power on the sacred altar
then activate the medallion at the moment of total eclipse."
Instead it will be more along the lines of "Push the button."

Curtis Dyer

unread,
Jan 25, 2012, 5:56:53 PM1/25/12
to
crankypuss <n...@email.thanks> wrote:

> I'm sure there's a way to do this, probably some simple syntax
> I've not run into and am too stupid to find in the manual or
> through google-fu or otherwise.
>
> I'd like to issue an arbitrary shell command in a subroutine and
> have it return an array that contains one element representing
> stdout and another element representing stderr.

Have a look at `proc_open()' for more fine-grained control of a
process:

<http://php.net/proc_open>


<snip>

--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>

Peter H. Coffin

unread,
Jan 26, 2012, 8:49:59 AM1/26/12
to
That, or some other shell depending (IIRC) what shell the user ID that
the script is running under, is what is going to parse the command you
pass to exec(). That, too, is not handled by magic.

--
Babbage pioneered IT management: his project took vast sums of cash, the spec
kept changing, and it ultimately failed to deliver. Turing scavenged supplies
and knocked together something that worked and solved the problem - reward?
Arrest and castration. Which one feels most like your career? -- Peter Corlett

crankypuss

unread,
Jan 27, 2012, 5:11:35 AM1/27/12
to
It looks like it might be what I'm looking for, but it will be a while
before I can get around to studying/trying it; thanks very much.

> Very off topic:
> Concerning your wish to intergrate PHP with a windowing environment, I
> found this:
>
> http://en.wikipedia.org/wiki/PHP-Qt
>
> I haven't used it myself, only with C++ so I have no clue how good it
> all is, but QT is solid.

Yes, I found that several days ago. It's object oriented, which is
rather a turnoff for me since all my PHP is currently strictly
procedural (mostly because eventually I'll port it to C, not C++, and
will probably do the bulk of that with a conversion program), there's no
repository that looks clearly trustworthy, and I don't care to build it
from source (because at this point in time I am too stoopid to do that
and have too much other stuff going on).

I'm keeping my eyes open but my current thinking is to either build a
pseudo-browser (based on gekko?) or modify Firefox about the head and
shoulders until it knows how to invoke PHP the way apache invokes it.

Thanks much.

crankypuss

unread,
Jan 27, 2012, 5:16:22 AM1/27/12
to
On 01/25/2012 03:25 PM, Jerry Stuckle wrote:
> On 1/25/2012 2:47 PM, crankypuss wrote:
>> I'm sure there's a way to do this, probably some simple syntax I've not
>> run into and am too stupid to find in the manual or through google-fu or
>> otherwise.
>>
>> I'd like to issue an arbitrary shell command in a subroutine and have it
>> return an array that contains one element representing stdout and
>> another element representing stderr.
>>
>> For example, using tar to deal with a bazillion files, error messages
>> might not be seen when specifying verbose output; I'd like to collect
>> them and display after stdout.
>>
>> I've not found any syntax, yet, for redirecting to a PHP variable rather
>> than some file.
>>
>> tia.
>
> Take a look at the proc_open(). The examples show how do return stdout
> and stderr in separate streams.

Thanks Jerry, someone else mentioned that too, and it looks like it will
probably do the trick though I'm not sure how soon I'll be able to play
with it.

> It may be more than you need (this is made to be interactive between the
> PHP script and the process), but you don't HAVE to send data to the
> process.

I've never minded putting large amounts of effort into building
something with a simple general purpose interface, those efforts tend to
repay tenfold.

(On a personal note, haven't you retired yet? Or are you like me,
addicted to code for life?)

crankypuss

unread,
Jan 27, 2012, 5:22:19 AM1/27/12
to
On 01/25/2012 03:52 PM, Peter H. Coffin wrote:
> On Wed, 25 Jan 2012 12:47:11 -0700, crankypuss wrote:
>> I'm sure there's a way to do this, probably some simple syntax I've not
>> run into and am too stupid to find in the manual or through google-fu or
>> otherwise.
>>
>> I'd like to issue an arbitrary shell command in a subroutine and have it
>> return an array that contains one element representing stdout and
>> another element representing stderr.
>>
>> For example, using tar to deal with a bazillion files, error messages
>> might not be seen when specifying verbose output; I'd like to collect
>> them and display after stdout.
>>
>> I've not found any syntax, yet, for redirecting to a PHP variable rather
>> than some file.
>
> Sorry, you have to use the file, then read the file if you want to parse
> back in what went out on stderr.

Actually no, I do not have to use no steenking files, I might have to
start writing something from scratch in freaking assembler, but I don't
have to use no steenking files to do something that hasn't the least
thing to do with files.

Thank you though, and apologies for any crankiness that has leaked into
my post... I'm going through the transition from many years of Windows
to using Linux, and although I mostly like it, I find the tendency for
scripts to rely on command output run through pipes to be abhorrent
since the whole concept of validity checking goes out the window when
you swallow that paradigm, and writing a file when it isn't necessary to
the actual process but rather is a matter of conformance to sloppiness
in the environment is at least as bad imo.

crankypuss

unread,
Jan 27, 2012, 5:23:23 AM1/27/12
to
On 01/25/2012 03:56 PM, Curtis Dyer wrote:
> crankypuss<n...@email.thanks> wrote:
>
>> I'm sure there's a way to do this, probably some simple syntax
>> I've not run into and am too stupid to find in the manual or
>> through google-fu or otherwise.
>>
>> I'd like to issue an arbitrary shell command in a subroutine and
>> have it return an array that contains one element representing
>> stdout and another element representing stderr.
>
> Have a look at `proc_open()' for more fine-grained control of a
> process:
>
> <http://php.net/proc_open>
>
>
> <snip>

Third recommendation for proc_open() this morning, you guys looking at
each others' notes? <g> Thanks.

Erwin Moller

unread,
Jan 27, 2012, 5:42:19 AM1/27/12
to
I was wondering that too.
When I posted my suggestion, I didn't see any others suggesting it.
But today I saw that Jerry and Curtis wrote it one day earlier.
I can imagine I had some mental black-out, but this looks like a mondial
one. ;-)

M. Strobel

unread,
Jan 27, 2012, 6:25:32 AM1/27/12
to
Can you expand on that? There must be a misunderstanding, the command you run does
all validity checking, me thinks.

/Str.

Jerry Stuckle

unread,
Jan 27, 2012, 8:49:08 AM1/27/12
to
Nope, still too young to retire. I don't know what I'd do with myself :)

Erwin Moller

unread,
Jan 27, 2012, 10:00:26 AM1/27/12
to
Get a hobby, like computer programming. ;-)

Jerry Stuckle

unread,
Jan 27, 2012, 10:12:08 AM1/27/12
to
ROFL!

Ah, that's part of the problem, Erwin. I have a hobby. But around
here, airplane rental is $140/hr and up.

Maybe what I need is a cheaper hobby :)

Peter H. Coffin

unread,
Jan 27, 2012, 11:37:31 AM1/27/12
to
100LL ain't cheap either.

--
73. I will not agree to let the heroes go free if they win a rigged
contest, even though my advisors assure me it is impossible for
them to win.
--Peter Anspach's list of things to do as an Evil Overlord

crankypuss

unread,
Jan 28, 2012, 7:00:11 AM1/28/12
to
LOL, back in college I had a couple roommates who were heavily into
learning to fly. I decided that flying was probably a lot like heroin,
"too good to try". <g>

crankypuss

unread,
Jan 28, 2012, 7:03:14 AM1/28/12
to
Look at any bash script longer than about 4 lines. You'll find it
issuing some command and running it through sed or whatever and then
through the next stage of the pipe and basically *assuming* that command
output formats never change and nothing can go wrong go wrong.

Peter H. Coffin

unread,
Jan 28, 2012, 12:40:28 PM1/28/12
to
That's just how good unix utilties do their work. They never change
output formats except by some kind of request, usually a command option.
The defaults freeze at release, and if there's a different default
output, the utility name will change as well.

--
"Every new technology carries with it an opportunity to invent a new
crime." --Laurence Urgenson (an assistant chief US attorney),
speaking in 1987 about the first arrests for what was later called
cellphone cloning.

M. Strobel

unread,
Jan 28, 2012, 6:40:12 PM1/28/12
to
It just fits. And no magic, only some basic principles.

/Str.

Curtis Dyer

unread,
Jan 28, 2012, 11:56:54 PM1/28/12
to
Erwin Moller <erwinmol...@xs4all.nl> wrote:

> On 1/27/2012 11:23 AM, crankypuss wrote:
>> On 01/25/2012 03:56 PM, Curtis Dyer wrote:
>>> crankypuss<n...@email.thanks> wrote:

<snip>

>>>> I'd like to issue an arbitrary shell command in a subroutine
>>>> and have it return an array that contains one element
>>>> representing stdout and another element representing stderr.
>>>
>>> Have a look at `proc_open()' for more fine-grained control of
>>> a process:
>>>
>>> <http://php.net/proc_open>
>>>
>>>
>>> <snip>
>>
>> Third recommendation for proc_open() this morning, you guys
>> looking at each others' notes? <g> Thanks.
>
> I was wondering that too.
> When I posted my suggestion, I didn't see any others suggesting
> it. But today I saw that Jerry and Curtis wrote it one day
> earlier. I can imagine I had some mental black-out, but this
> looks like a mondial one. ;-)

In my case, I often get distracted before finishing a post and
usually end up forgetting to fetch new headers. I suppose it's a
good sign for the OP when several people agree on an approach.

Jerry Stuckle

unread,
Jan 30, 2012, 9:37:29 PM1/30/12
to
On 1/27/2012 11:37 AM, Peter H. Coffin wrote:
> On Fri, 27 Jan 2012 10:12:08 -0500, Jerry Stuckle wrote:
>> On 1/27/2012 10:00 AM, Erwin Moller wrote:
>>> Get a hobby, like computer programming. ;-)
>>>
>>> Regards,
>>> Erwin Moller
>>>
>>
>> ROFL!
>>
>> Ah, that's part of the problem, Erwin. I have a hobby. But around
>> here, airplane rental is $140/hr and up.
>>
>> Maybe what I need is a cheaper hobby :)
>
> 100LL ain't cheap either.
>

Around $5.30/gal, which is pretty good. It used to be about 2.5 or 3x
87 octane unleaded (auto).

crankypuss

unread,
Feb 1, 2012, 7:22:30 AM2/1/12
to
On 01/25/2012 12:47 PM, crankypuss wrote:
> I'm sure there's a way to do this, probably some simple syntax I've not
> run into and am too stupid to find in the manual or through google-fu or
> otherwise.
>
> I'd like to issue an arbitrary shell command in a subroutine and have it
> return an array that contains one element representing stdout and
> another element representing stderr.
>
> For example, using tar to deal with a bazillion files, error messages
> might not be seen when specifying verbose output; I'd like to collect
> them and display after stdout.
>
> I've not found any syntax, yet, for redirecting to a PHP variable rather
> than some file.
>
> tia.

Thanks for your help, folks. This seems to do the trick:


// executes $cmd as a separate process and returns the following:
// $result = null if unable to start process, else:
// $result["exit_code"] = exit code returned by $cmd
// $result["stdout"] = stdout written by $cmd
// $result["stderr"] = stderr written by $cmd
function sysCmd($cmd)
{
$result = array();
$descr = array();
$descr[0] = array("pipe", "r"); // stdin
$descr[1] = array("pipe", "w"); // stdout
$descr[2] = array("pipe", "w"); // stderr
$proc = proc_open($cmd, $descr, $pipes);
if (is_resource($proc))
{
fclose($pipes[0]); // no stdin data
$result["stdout"] = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$result["stderr"] = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$result["exit_code"] = proc_close($proc);
}
return $result;
}
0 new messages