Hi,
I need to be able to safely execute local commands on both Unix and Win32 platforms in a way that captures both the STDERR and STDOUT from the command using Perl 5.6.1. The code must have a configurable timeout too.
Has anyone got any ideas about the best way to do this?
I had thought about something along these lines but I can't see a way of easily getting STDERR & STDOUT with this. Any suggestions for improvement are also most welcome
...
my $err;
my @output;
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alert 10;
my $pid = open(PIPE,"-|") or die "Error: $!";
exec @command_array or die "Error: $!";
while(<PIPE>) {
push(@output,$_);
}
close PIPE;
if(kill('TERM',$pid)) { warn "Killed PID $pid\n"; }
};
alarm 0;
if($@ && $@ ne "alarm\n") {
$err = "Non-timeout error: $@";
push(@output,$err);
} else {
$err = "Timeout error: $@";
push(@output,$err);
}