in reply to Execute shell command, grab it's return, terminate on timeout
This is a side note about code style suggestions
1) It really would be wise to use named parameters for all your methods. Your code would become self-documenting and you wouldn't have to preface your code with a description of what your parameters were when posting on perlmonks.
2) You also have a comment next to @cmd, describing it as a return value. Why not just name it @return_val? Then no need for a comment.
3) Finally, if you look at the documentation for alarm, you'll see a suggestion on how to propagate errors that don't revolve around the alarm. If your command fails on things other than the Timeout, you're probably going to want to know that.
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; } }
Good luck getting assistance with your requested problem,
- Miller
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Execute shell command, grab it's return, terminate on timeout
by Haioken (Novice) on Mar 21, 2014 at 03:08 UTC |