in reply to Perl script to run for x hours

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; } }