Looks like it's known
problem in perl.
Also, similar issue
Can't catch signals after an exec?
Code can be simplified to
$sub = sub { exec "perl $0" };
$SIG{INT} = $sub;
$SIG{USR1} = $sub;
while(1) {
print ++$counter, "\n";
sleep 1;
}
(if you press Ctrl-C, it restarts only once)
Workaround for example in perldoc:
#!/usr/bin/env perl
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
+.
print STDERR "STARTED\n";
my $script = File::Basename::basename($0);
my $SELF = catfile($FindBin::Bin, $script);
# POSIX unmasks the sigprocmask properly
my $need_restart = 0;
$SIG{HUP} = sub {
print "got SIGHUP\n";
$need_restart = 1;
};
code();
sub code {
print "PID: $$\n";
print "ARGV: @ARGV\n";
my $count = 0;
while (++$count) {
sleep 2;
if ($need_restart) {
exec($SELF, @ARGV) || die "$0: couldn't restart: $!";
}
print "$count\n";
}
}
(i.e. need to call exec() outside of signal handler)
And another workaround is posted above by McA
Re: Using SIGHUP to restart a daemon
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.