sub exec_safe { my ($command, $timeout, $nice_val) = @_; my @return_val; eval { local $SIG{ALRM} = sub { die "Timeout\n" }; alarm $timeout; @return_val= `nice -n $nice_val $command`; alarm 0; }; if($@) { # If command fails, return non-zero and msg die unless $@ eq "Timeout\n"; # propagate unexpected errors return ("Command timeout",1); } else { chomp @return_val; push(@return_val, $? >> 8); return @return_val; } }