There are several ways to do it.

Since you want something standalone, without ties to the caller, a seperate process seems best. That allows the parent to use any timers and alarms it likes.

Lets call the thing "Timelimit", and abuse import to set the limit and fork, per Errto.

package Timelimit; use warnings; use strict; =head1 NAME Timelimit - fork off a watchdog timer =cut =head1 SYNOPSIS use Timelimit $seconds; use Timelimit; # default 120 seconds =cut use vars qw/$N/; sub import { my $class = shift; $N = 0+$_[0] || 120; # default two minutes my $ppid = $$; defined(my $pid = fork) or die $!; return $pid if $pid; local $SIG{HUP} = sub { exit 0 }; while ($N > 0) { $N -= sleep $N; } kill INT, $ppid; # or, for maximum brutality, # kill KILL, $ppid; sleep 1 while kill 0, $ppid; exit 0; } 1; __END__ =head1 AUTHOR Zaxo, Sept 2005 =cut
The initial $Timelimit::N setting is visible to the user who wants to see it.

Update ++Errto's suggestion gratefully adopted.

After Compline,
Zaxo


In reply to Re: brutally stop a perl program if it runs too long by Zaxo
in thread brutally stop a perl program if it runs too long by water

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.