use constant MAX_ITERATIONS => 100000; use constant MAX_TIME => 6000; use warnings; use strict; my $start_time = time; my $iterations = 0; my $exit_requested = 0; my $handling_request = 0; sub sig_handler { $exit_requested = 1; print "Caught interrupt\n"; exit(3) if !$handling_request; } $SIG{TERM} = \&sig_handler; #init(); while (!$exit_requested && $iterations <= MAX_ITERATIONS && (time - $start_time) < MAX_TIME) { $handling_request = 1; handle_request(); $iterations++; $handling_request = 0; handlyn(); } exit(0); #sub init { return 1; } sub handle_request { handlyn(); print ("iterations: $iterations\n"); } sub handlyn { print $handling_request ? "" : "NOT ", "handling\n"; sleep 3; }