Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: Using SIGHUP to restart a daemon

by vsespb (Hermit)
on Oct 17, 2013 at 17:17 UTC ( #1058652=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Using SIGHUP to restart a daemon
in thread Using SIGHUP to restart a daemon

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


Comment on Re^3: Using SIGHUP to restart a daemon
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1058652]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2014-08-29 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls