For fun and learning last week, I wrote a timed event handler that can manage these things (there are FAR better ones on the CPAN, so look there if you want to go that route). There are many ways to do what you want, but as others have said, we need to know more info about what your script actually does. I just thought I'd throw this out there as an idea for one way.

In this example, I've used a while() loop to simulate the workload, but that isn't necessary. I'll comment in code as to what is happening. $limit is the amount of time in seconds you want to run your script for (32400 for 9 hours). $interval is how often you want to check the runtime (in seconds again).

use warnings; use strict; use Async::Event::Interval; # signal handler for when an interrupt signal is thrown $SIG{INT} = sub { cleanup(); }; my $limit = 10; my $interval = 3; my $parent_pid = $$; my @args = ($parent_pid, time(), $limit); # create the event in a separate process to 'monitor' the # runtime of the main (this) script my $event = Async::Event::Interval->new($interval, \&time_check, @args); $event->start; while (1){ # do stuff... sleep 1; } # this is our signal handler routine. It exits the main application. # the event automatically cleans itself up sub cleanup { print "time's up, exiting...\n"; exit; } # this is the event callback. It checks runtime, and when # reached, sends an interrupt signal to the parent process sub time_check { my ($parent_pid, $start_time, $limit) = @_; my $run_time = time - $start_time; if ($run_time >= $limit){ kill 'SIGINT', $parent_pid; } }

In reply to Re: Perl script to run for x hours by stevieb
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.