My group has been having intermittent problems with hanging processes. These programs are binaries and we don't have access to the source code to investigate what is causing the problems. The dead processes cause other problems that I won't go into, but suffice to say that we need an automated way of killing them when they hang.

Today I hacked together this wrapper script with the simple idea that if the problem binary's logfile hasn't been updated in 3 minutes, the process is dead, and is safe to kill. This is our standard criteria for killing this process.

It works famously in testing, but before I migrate it, I wanted to get some feedback about using the alarm function the way I am. Is it safe to reset the alarm in the $SIG{ALRM} intercept? Is it possible for $SIG{ALRM} to not get called after the timeout? Are there handy modules I've never stumbled across that account for wacky edge cases?

This is for AIX Unix with Perl 5.6.0.

my $timeout = 60; my $logfile = 'logfile.txt'; my @array = ('the_binary -option1', 'the_binary -option2', 'the_binary + -option3'); for (@array) { print "Running command $_\n"; eval { local $SIG{ALRM} = sub { my $mod_time = time - (stat($logfile))[9]; if ( $mod_time > 180 ) { die "alarm\n"; } else { alarm 0; alarm $timeout; } }; alarm $timeout; system($_); alarm 0; }; }

In reply to Safety of using alarm by delirium

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.