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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |