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