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


In reply to Re: Execute shell command, grab it's return, terminate on timeout by wind
in thread Execute shell command, grab it's return, terminate on timeout by Haioken

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.