#!/usr/bin/perl use strict; use Symbol; use IO::Select; use IPC::Open3; my $outlog = shift; my $errlog = shift; my $cmd = shift; my @args = @ARGV; open OUTLOG, ">>$outlog" or die "open $outlog: $!"; open ERRLOG, ">>$errlog" or die "open $errlog: $!"; my ( $rdr, $wtr, $err ); my $pid = open3( $wtr, $rdr, $err = Symbol::gensym, $cmd, @args ); close $wtr; my $select = IO::Select->new( $rdr, $err ); my $outstr = ''; my $errstr = ''; while ( $select->handles ) { for my $handle ( $select->can_read ) { my $bytes = sysread $handle, my ($str), 1024; die "sysread: $!" unless defined $bytes; $select->remove($handle), next unless $bytes; if ( fileno($handle) == fileno($rdr) ) { print STDOUT $str; print OUTLOG $str; $outstr .= $str; } elsif ( fileno($handle) == fileno($err) ) { print STDERR $str; print ERRLOG $str; $errstr .= $str; } } } close $rdr; close $err; close OUTLOG; close ERRLOG; waitpid $pid, 0; my $returncode = $? >> 8; my $signal = $? & 127; my $coredump = $? & 128; exit unless $returncode; my $time = localtime; my $mail = \*STDERR; # new Mail::Mailer; print $mail <<END; Process $cmd @args returned $returncode (signal $signal, coredump $coredump) at $time standard output: ==== $outstr ==== standard error: ==== $errstr ==== END close $mail;

In reply to Re: Multiplexing STDOUT and STDERR by Anonymous Monk
in thread Multiplexing STDOUT and STDERR by kilinrax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.