Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning STDERR value with Shell_Exec 1

Status
Not open for further replies.

Guggly

Programmer
Joined
Jan 18, 2004
Messages
110
Location
US
Hi! How can I get the value of STDERR for a program that I’ve launched using Shell_Exec? I can output STDERR to a file using >FILENAME but I’d like to be able to have the STDERR value sent back directly to a variable. Thanks!
 
Thanks, but how can I get it to return the value of stderr without writing it to a file? If I change the script so that $descriptorspec is as follows:

Code:
$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("pipe", "a")
);

When I call "echo($pipes[2]);" I just get a resource ID as the return. What am I doing wrong here?

Also, do I need to explicitly close pipes 0 and 1 if I never read or write to them? All I'm interested is in stderr. Thanks!
 
That's because $pipes[0], $pipes[1], and $pipes[2] are all resource handles that you treat as file handles.

Look at the example code for what it does with $pipes[1].

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I've tried using the same syntax for $pipes[2] but nothing gets returned. When I use the original "file" setting, sderr value is being written to the file. Am I missing something?

Code:
while (!feof($pipes[2])) {
       echo fgets($pipes[2], 1024);
   }
 
Are you sure that the app you're invoking using proc_open() is sending anything to stderr?

Here's my test PHP script, based on the code from the online manual page:

Code:
<?php
$descriptorspec = array
(
   1 => array("pipe", "w"),
   2 => array("pipe", "w")
);

$process = proc_open("./foo", $descriptorspec, $pipes);

if (is_resource($process))
{
   while (!feof($pipes[1]))
   {
       $a .= fgets($pipes[1], 1024);
   }
   fclose($pipes[1]);
   
   while (!feof($pipes[2]))
   {
       $b .= fgets($pipes[2], 1024);
   }
   fclose($pipes[2]);
   
   $return_value = proc_close($process);

   print "command returned $return_value\n";
   print 'stdout from program: ' . $a ."\n";
   print 'stderr from program: ' . $b ."\n";
}
?>

The app "foo" that script invokes is a c-language program, the source code of which consists of:

Code:
#include <stdio.h>

int main()
{
	fprintf (stdout, "To STDOUT:  test test test\n");
	fprintf (stderr, "To STDERR:  tset tset tset\n");

	return 19;
}

When I invoke my PHP script, the output I get is:

Code:
command returned 19
stdout from program: To STDOUT:  test test test

stderr from program: To STDERR:  tset tset tset

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Okay, I found the problem, the stderr pipe needs to use "w" (like you had it) in it's array instead of the original "a":

I had it like this:
$descriptorspec = array
(
2 => array("pipe", "a")
);

It should be like this:
$descriptorspec = array
(
2 => array("pipe", "w")
);

If you're sending stderr to a file it doesn't seem to make a difference what that second value is.

Thanks for your help!
 
If you're outputting STDERR to a file, it will matter whether there's an "a" or "w". But only on successive invocations of the script.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
That's good to know. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top