From: Stas Bekman Date: Thu, 30 May 2002 15:29:02 +0000 (+0800) Subject: Handling the SIGHUP Signal in Daemons X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=28494392d67f95ee74740353c8e635f622ffe336;p=p5sagit%2Fp5-mst-13.2.git Handling the SIGHUP Signal in Daemons Message-ID: <3CF5D4BE.4000500@stason.org> p4raw-id: //depot/perl@16876 --- diff --git a/pod/perlipc.pod b/pod/perlipc.pod index 3b997ac..3af062f 100644 --- a/pod/perlipc.pod +++ b/pod/perlipc.pod @@ -168,6 +168,66 @@ module. Lamentably, this is almost entirely undocumented, but the F file from the Perl source distribution has some examples in it. +=head2 Handling the SIGHUP Signal in Daemons + +A process that usually starts when the system boots and shuts down +when the system is shut down is called a daemon (Disk And Execution +MONitor). If a daemon process has a configuration file which is +modified after the process has been started, there should be a way to +tell that process to re-read its configuration file, without stopping +the process. Many daemons provide this mechanism using the C +signal handler. When you want to tell the daemon to re-read the file +you simply send it the C signal. + +The implementation of such a mechanism in Perl using a normal signal +handler works only the first time the signal is sent. The solution to +this problem is to use C signal handlers if available. + +The following example implements a simple daemon, which restarts +itself every time the C signal is received. The actual code is +located in the subroutine C, which simply prints some debug +info to show that it works and should be replaced with the real code. + + #!/usr/bin/perl -w + + use POSIX (); + use FindBin (); + use File::Basename (); + use File::Spec::Functions; + + $|=1; + + # make the daemon cross-platform, so exec always calls the script + # itself with the right path, no matter how the script was invoked. + my $script = File::Basename::basename($0); + my $SELF = catfile $FindBin::Bin, $script; + + # POSIX unmasks the sigprocmask properly + my $sigset = POSIX::SigSet->new(); + my $action = POSIX::SigAction->new('sigHUP_handler', + $sigset, + &POSIX::SA_NODEFER); + POSIX::sigaction(&POSIX::SIGHUP, $action); + + sub sigHUP_handler { + print "got SIGHUP\n"; + exec($SELF, @ARGV) or die "Couldn't restart: $!\n"; + } + + code(); + + sub code { + print "PID: $$\n"; + print "ARGV: @ARGV\n"; + my $c = 0; + while (++$c) { + sleep 2; + print "$c\n"; + } + } + __END__ + + =head1 Named Pipes A named pipe (often referred to as a FIFO) is an old Unix IPC