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); } #### 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"; }