As noted by others, what the script is doing generally drives the decision on how to implement the time limit.

That said, if you have something that can run for hours, one hopes it is doing some menial task inside a loop. The most graceful (but to Happy-the-monk's point, not necessarily the most timely) approach, in my opinion, is to grab the time at the start of the loop, precompute the ending time, and then check current time at the top of the loop and see if we've passed the deadline.

Something like this:

#!/usr/bin/perl use strict; use warnings; my $run_limit_hours = 9; # You would of course grab this +from @ARGV or something my $start_time = time; # Number of seconds since epoch my $end_time = # Deadline -- end time in second +s-since-epoch $start_time + $run_limit_hours * 60 * 60; # 60 seconds per minute, 60 minu +tes per hour while (1) { my $current_time = time; if ($current_time > $end_time) { last; } &doTheThing(); } exit;


In reply to Re: Perl script to run for x hours by marinersk
in thread Perl script to run for x hours by t-rex

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.