in reply to Handling TERM signal
You can either either ignore a signal instance or handle it, but not both. Only future signals will be affected by your $SIG{TERM} = "IGNORE".
use Time::HiRes qw( sleep ); # Optional. print("Process Id = $$\n"); print("Start time = " . localtime() . "\n"); my $term_sig; local $SIG{TERM} = sub { $SIG{TERM} = "IGNORE"; $term_sig = 1; }; # Sleep for 20 seconds. Ignore interruptions. my $sleep_til = time() + 20; for (;;) { my $sleep_len = $sleep_til - time(); last if $sleep_len <= 0; sleep($sleep_len); } print("End time = " . localtime() . "\n"); if ($term_sig) { print "Received TERM signal\n" } else { print "Did not receive TERM signal\n" }
Tested:
$ 603944.pl Process Id = 65138 Start time = Fri Mar 9 01:06:18 2007 End time = Fri Mar 9 01:06:38 2007 Did not receive TERM signal $ 603944.pl Process Id = 65236 Start time = Fri Mar 9 01:07:00 2007 $ kill -s TERM 65236 End time = Fri Mar 9 01:07:20 2007 Received TERM signal
|
|---|