In general, the 'eval/alarm' solution can not be used when you run backticks or system since the child doesn't always get the hint and die. It works ok on internal perl commands. See my first PM post here.

I'm not sure why you are getting 56 timeouts. Just for kicks, try doing a 'ps' during this and see if you are accumulating child processes (and eventually reach your limit).

Since then I've tried other solutions, but can't say I've ever come across what I thought was especially clean. I don't like using signals much since they are unsafe and I'm paranoid (as my previous post details). I tend to fork/exec the process off, send the output to a temp file, and then (lamely) do a nonblocking waitpid call in a polling loop where I sleep 1 second each time through to check on it. You can replace the waitpid loop with something (untested) like ...

eval { local $SIG{ALRM} = sub { die 'timedout' }; alarm $time_to_wait; waitpid($pid, 0); $exit_code = $?; alarm(0); } if ($@) { $exit_code = ($@ =~ /timedout/)? 100 : 200; }
... but why bother? (The polling solution does keep your process in memory during the wait, and uses a small amount of CPU time.) FWIW, you can also do the same kind of thing without a temp file by redirecting output to a pipe to the parent (if you are careful).

bluto


In reply to Re: timeout process problems by bluto
in thread timeout process problems by shrubbery

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.