PERL IPC Process Hangs - Need help reading STDERR & STDOUT

Hello,

I've been playing around with IPC::Open3 for a few days now, getting better at it, but still just guessing in the dark really since I fairly new to this stuff on the PERL side of things.  I am trying to use PsExec to send remote commands to remote hosts in succession via the open3 command, and capture the errors and output to check for success or failure, so I can report on the status of each script being executed.

Basically if I set the while <CATCHERR> statement to a closed loop with nothing inside, the script runs... BUT if try to read $_ inside the while <CATCHERR> {} statement block, the script hangs and does not return.  How do I properly redirect STDERR to a temporary file CATCHERR, and then read the contents of that file to check on the output of my  command?

foreach my $line (@lines) {

            if ($line =~ m/((^[\w\d:\\\_\-\.]{1,})$)/sim) {
                  $target = $2;
            } else {
                  $target = "";
            }

       $cmd = 'psexec \\\\'.$target.' -u '.$user.' -p '.$pass.' -n 45 -c -f "'.$datafolder.$batch.'" -e';

        local *CATCHOUT = IO::File->new_tmpfile;
        local *CATCHERR = IO::File->new_tmpfile;
        local *CATCHIN = IO::File->new_tmpfile;
        my $pid = open3(gensym, \*CATCHOUT, ">&CATCHERR", $cmd);

        while( <CATCHOUT> ) { }  # closed the block to make it run, but I want to read the output into an array

#              push (@output, $_);
#              if ($debug) {
#                    print $_."<br />\n";
#              }
#              }

      waitpid($pid, 0);
        seek CATCHERR, 0, 0;
        while( <CATCHERR> ) { } # this by itself works ok, returns properly, but I can't read the results

        # this is what causes the code to hang, having a read of $_ while inside the while <CATCHERR> block

        seek CATCHERR, 0, 0;
        while( <CATCHERR> ) { # this is closer to what I want, but fails to return, always hangs on 2nd attempt

              if ($_ =~ m/error code (\d{1,})/) {
                        $result = $1
                  } else {
                        $result = -1;
                  }

                  if ($_ =~ m/^[\w\d\s\W]{3,}$/im) {
                $lastline .= $_;
                if ($debug) {
                      print "lastline = ".$lastline."<br />\n";
                      print "\$_ = ".$_."<br />\n";
                      }
                  }
            }

} #end foreach @line


OK, if someone could please tell me how to properly redirect the STDERR & STDOUT to files, read the files into an array, and prevent the script from hanging in the process, I would be most appreciative.

Thanks in advance...
G
ghostingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nedfineCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ghostingAuthor Commented:
OK,

I have been playing around with variations on a theme from your suggested link...

No luck...

I can redirect STDERR & STDOUT...  BUT the script exits before completing execution and I lose STDOUT all together after having redirected it once.  How do I selectively redirect STDOUT & STDERR to a file, then flush their contents, return control back to STDERR & STDOUT, so I can read that file in using a different file handle to check my results, and repeat this process many times?

Here's a stripped down version of my code after many revisions....

open (OUTPUT, ">output.txt") || die $!;
#open (INPUT, "input.txt")    || die $!;
open (ERROR, ">error.txt")   || die $!;

STDOUT->fdopen(\*OUTPUT, "w") || die $!;
#STDIN->fdopen(\*INPUT, "r")   || die $!;
STDERR->fdopen(\*ERROR, "w")  || die $!;


system ($cmd);

select (STDOUT);
#select (STDERR);
$| = 1;

close (OUTPUT);
close (ERROR);

The BIG problem is STDOUT & STDERR are not returning to their normal states, so my script simply says it's done, but keeps running without my knowing what's happening.  Please illuminate me on how to correct this code snippet so it will work for me...

Thanks in advance,
G
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.