Scenarios: 爲daemon process重新加載配置
Solution: 可通過接收SIGHUP,並定義操作。如下實現了向 a daemon process 發送SIGHUP,並restart該process。當然,也可以是加載配置。
#!/usr/bin/perl
use strict;
use warnings;
use POSIX ();
use FindBin ();
use File::Basename ();
use File::Spec::Functions qw(catfile);
$| = 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
$SIG{HUP} = sub {
print "got SIGHUP\n";
exec($SELF, @ARGV) || die "$0: couldn't restart: $!";
};
code();
sub code {
print "PID: $$\n";
print "ARGV: @ARGV\n";
my $count = 0;
while (1) {
sleep 2;
print ++$count, "\n";
}
Start a pty on your host, and run below daemon program:
[root@localhost tmp]# perl test_sighup.pl
PID: 27460
ARGV:
1
Then start another pty, and send SIGHUP:
[root@localhost ~]# kill -1 27460
Back to the former pty, will see:
[root@localhost tmp]# perl test_sighup.pl
PID: 27460
ARGV:
1
2
3
got SIGHUP