Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Multiplexing STDOUT and STDERR

by Anonymous Monk
on Nov 13, 2003 at 18:26 UTC ( [id://306883]=note: print w/replies, xml ) Need Help??


in reply to Multiplexing STDOUT and STDERR

#!/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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://306883]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-18 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found