in reply to Re: Gracefully exiting daemons
in thread Gracefully exiting daemons

I could have a global variable being updated throughout the script, with the current state of execution, and in case the signal is trapped, I can then act/log based upon it...

Almost there, but not entirely what I wanted... Isn't there a way to actually delay the execution of the sub as the signal is received? That would allow the script to finish whatever it was doing at the time (if it was doing anything), and only then die gracefully...

Replies are listed 'Best First'.
Re: Re: Re: Gracefully exiting daemons
by Brovnik (Hermit) on Jun 26, 2001 at 02:32 UTC
    You can of course catch the signal and do nothing, or (as here) just set a variable for you to look at later.
    $SIG{HUP} = \&catch; my $caught = 0; my $count = 0; print "I am $$, HUP me\n"; for (0..10) { sleep (5); # pretending to do stuff print "$_\n"; } die "I was HUPed $caught times" if $caught; ### Subroutines below sub catch { $caught++; }
    You should of course think carefully about ignoring signals, expecially INT and TERM, since if you get these, someone/something is trying to tell you something.

    But, delaying handling e.g. HUP or USR1 until it is a better time to handle is OK.

    Make sure you document the signal handling well, specially if it is not expected behaviour. E.g. HUP is often used to mean "Please reread your config file".
    --
    Brovnik