Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Perl script to run for x hours

by marinersk (Priest)
on Sep 28, 2016 at 02:45 UTC ( [id://1172790]=note: print w/replies, xml ) Need Help??


in reply to Perl script to run for x hours

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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1172790]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-18 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found