From: Brandon L Black Date: Thu, 20 Dec 2007 20:52:10 +0000 (+0000) Subject: only remove pidfile in the stop() function on successful kill -9, let daemon do it... X-Git-Tag: 0_06~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b6b69acaef633259ece4dcf3c62625d3d5097965;p=gitmo%2FMooseX-Daemonize.git only remove pidfile in the stop() function on successful kill -9, let daemon do it otherwise, so we don't loose track of a daemon --- diff --git a/lib/MooseX/Daemonize.pm b/lib/MooseX/Daemonize.pm index dc6be99..ed7dc5c 100644 --- a/lib/MooseX/Daemonize.pm +++ b/lib/MooseX/Daemonize.pm @@ -223,15 +223,6 @@ sub stop { } } - - # clean up ... - eval { $self->pidfile->remove }; - if ($@) { - warn "Could not remove pidfile (" - . $self->pidfile->file - . ") because : $!"; - } - } else { # this just returns the OK @@ -265,22 +256,38 @@ $_kill = sub { # $!{EPERM} could also be true if we cant kill it (permission error) # Try SIGINT ... 2s ... SIGTERM ... 2s ... SIGKILL ... 3s ... UNDEAD! + my $terminating_signal; for ( [ 2, $timeout ], [15, $timeout], [9, $timeout * 1.5] ) { my ($signal, $timeout) = @$_; $timeout = int $timeout; CORE::kill($signal, $pid); - last unless CORE::kill 0 => $pid or $!{EPERM}; - while ($timeout) { - sleep(1); - last unless CORE::kill 0 => $pid or $!{EPERM}; + unless(CORE::kill 0 => $pid or $!{EPERM}) { + $terminating_signal = $signal; + last; + } $timeout--; + sleep(1) if $timeout; } + + last if $terminating_signal; } - return unless ( CORE::kill 0 => $pid or $!{EPERM} ); + if($terminating_signal) { + if($terminating_signal == 9) { + # clean up the pidfile ourselves iff we used -9 and it worked + warn "Had to resort to 'kill -9' and it worked, wiping pidfile"; + eval { $self->pidfile->remove }; + if ($@) { + warn "Could not remove pidfile (" + . $self->pidfile->file + . ") because : $!"; + } + } + return; + } # IF it is still running Carp::carp "$pid doesn't seem to want to die."; # AHH EVIL DEAD!