I have a program that is supposed to run a set of other programs and collect their output for logging purposes. Everything is working fine as long as there is output to standard out.
That leads me to my two problems:
How can I capture both
STDIN
andSTDERR
of another program in one file?If there is no output at all (or output to
STDERR
only) the program will get stuck on the line:while (<$input>)
How can I make the program wait for possible output from another program of indeterminate run time, and still continue if there is no output by the time the program finishes execution.
Here is that section of the code
my $pid = open (my $input, '-|', "$prog $args")
or push @errors, "A failute has occurred in $prog $args";
if(not @errors){
while (<$input>){ #POSSIBLE LOCATION FOR HANG UP IF NO PRINTING IS DONE
if($input =~ /^END\n$/){
last;
}
print $fh $_;
}
}
else{
print $fh "An error has occurred with executing \"$prog $args\"";
}
note: $fh
is my file handler used to write to my log file, and @errors
is used for internally reporting errors in my program.
EDIT:
One problem I experienced with trying the Proc::Reliable route is that it seems to have after effects on STDOUT
and STDERR
, and I would need to fix those after each call to run. This leads to another problem in my code. I am running in parallel and any change to STDOUT
and STDERR
effect all of the threads (using Windows at least).
IO::CaptureOutput has the same issue for me, but it tries to correct the STDOUT
and STDERR
jumbling internally and causes the following error to occur:
Couldn't remove temp file 'C:\Users\SBLAKE~1\AppData\Local\Temp\m8E3pUGfOS' - Permission denied at C:/Perl/site/lib/IO/CaptureOutput.pm line 82