diff options
author | Stas Bekman <stas@stason.org> | 2002-05-30 23:29:02 +0800 |
---|---|---|
committer | Jarkko Hietaniemi <jhi@iki.fi> | 2002-05-30 12:29:09 +0000 |
commit | 28494392d67f95ee74740353c8e635f622ffe336 (patch) | |
tree | e2e30a6c40bb68d295759fc09e625037a6fe53a1 /pod/perlipc.pod | |
parent | 15b7a6a87bd774f291d56fb64397a74d0ed2dd47 (diff) | |
download | perl-28494392d67f95ee74740353c8e635f622ffe336.tar.gz |
Handling the SIGHUP Signal in Daemons
Message-ID: <3CF5D4BE.4000500@stason.org>
p4raw-id: //depot/perl@16876
Diffstat (limited to 'pod/perlipc.pod')
-rw-r--r-- | pod/perlipc.pod | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/pod/perlipc.pod b/pod/perlipc.pod index 3b997ac444..3af062f6b4 100644 --- a/pod/perlipc.pod +++ b/pod/perlipc.pod @@ -168,6 +168,66 @@ module. Lamentably, this is almost entirely undocumented, but the F<t/lib/posix.t> 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<SIGHUP> +signal handler. When you want to tell the daemon to re-read the file +you simply send it the C<SIGHUP> 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<POSIX> signal handlers if available. + +The following example implements a simple daemon, which restarts +itself every time the C<SIGHUP> signal is received. The actual code is +located in the subroutine C<code()>, 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 |