Hi all,
I'm trying to write a function that runs a command line (like say .... ls -l) and captures:
* its STDOUT and STDERR output
* its exit status
* the time it took to execute
The last two objectives are fairly easy but I'm having trouble with the first one. I know that running a command thu a pipe ( open( H_PROGRAM, "ls -l |" ); ) only captures its STDOUT but not its STDERR, so here is how I've been trying to work around that:
1) Create a unidirectional pipe.
2) Fork.
3) In the child process, close STDOUT/STDERR and make them synonyms for the write-end of my pipe.
4) In the child process, exec() the command line.
5) In the parent process, wait for the child to finish and then read whatever got written to the pipe.
However, this does not seem to work the way I want it, and nothing gets written to the pipe. I'm thinking maybe exec() doesn't work the way I thought it would. Could it be that exec(), before it runs the actual command, nullifies my redirection of STDOUT/STDERR so they're not actually associated with the write-end of my pipe, as I thought they would be?
Any helpful replies welcome.
Kind regards,
antred (Markus)
P.S. Code in second post.
I'm trying to write a function that runs a command line (like say .... ls -l) and captures:
* its STDOUT and STDERR output
* its exit status
* the time it took to execute
The last two objectives are fairly easy but I'm having trouble with the first one. I know that running a command thu a pipe ( open( H_PROGRAM, "ls -l |" ); ) only captures its STDOUT but not its STDERR, so here is how I've been trying to work around that:
1) Create a unidirectional pipe.
2) Fork.
3) In the child process, close STDOUT/STDERR and make them synonyms for the write-end of my pipe.
4) In the child process, exec() the command line.
5) In the parent process, wait for the child to finish and then read whatever got written to the pipe.
However, this does not seem to work the way I want it, and nothing gets written to the pipe. I'm thinking maybe exec() doesn't work the way I thought it would. Could it be that exec(), before it runs the actual command, nullifies my redirection of STDOUT/STDERR so they're not actually associated with the write-end of my pipe, as I thought they would be?
Any helpful replies welcome.
Kind regards,
antred (Markus)
P.S. Code in second post.