in reply to Launching an external process with a run-time limit
One downside is that I haven't manged to shave the yak to get capturing of both STDOUT and STDERR to work on both Windows and *n*x.sub texec { my $timeout = shift; my $out = []; eval { local $SIG{ALRM} = sub { die "timeout\n" }; alarm ($timeout); # set timer .. hope the platform has signals my $cpid = open (my $fh, "-|") || exec (@_); $| = 1; # line buffer while (<$fh>) { chomp; push (@{$out}, $_); } close ($fh); alarm 0; # reset, we've got all the data and we're cool }; # If something died in the eval, return a ref to a list containing # the output as well as the die text. otherwise just the output. return (($@) ? [$out, $@] : $out); }
Usage is:
my $res = texec (10, "/usr/bin/ls", "-al", "/"); if (ref ($res->[0])) { print "Error while executing: $res->[1]\nCommand output:\n"; print Dumper ($res->[0]), "\n"; } else { print "Command output:\n"; print Dumper ($res), "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Launching an external process with a run-time limit
by Anonymous Monk on Oct 18, 2006 at 15:40 UTC | |
|
Re^2: Launching an external process with a run-time limit
by TheFluffyOne (Beadle) on Oct 18, 2006 at 15:43 UTC |