I took your code and added a sub that i can test and also changed your TERM to INT so it is easy for me to test
#!/usr/bin/perl -w use constant MAX_ITERATIONS => 1000000; use constant MAX_TIME => 600; 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(0) if !$handling_request; } sub handle_request { print ("hi: $iterations\n") if $iterations % 10000 == 0; } $SIG{INT} = \&sig_handler; # init(); while (!$exit_requested && $iterations <= MAX_ITERATIONS && (time - $s +tart_time) < MAX_TIME) { $handling_request = 1; handle_request(); $iterations++; $handling_request = 0; } print ("Exit value: Iteration = $iterations exit_req = $exit_requested +\n");
Output Here when I run the script and hit ^C while it is running I get this output
Seems to suggest that it picked the interrupt and terminated the loop at that point.hi: 0 hi: 10000 hi: 20000 hi: 30000 hi: 40000 hi: 50000 hi: 60000 hi: 70000 hi: 80000 hi: 90000 hi: 100000 hi: 110000 hi: 120000 hi: 130000 Caught interrupt Exit value: Iteration = 133660 exit_req = 1
Is this the behavior you want and you don't see that? Are you sure you are sending TERM signals?
-SK
In reply to Re: Controlling a Perl Daemon with Signals
by sk
in thread Controlling a Perl Daemon with Signals
by eastcoastcoder
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |