From 83d5f55b09150818b31c611df48a177cd384ca7d Mon Sep 17 00:00:00 2001 From: Scott Walters Date: Wed, 19 May 2010 07:52:43 -0400 Subject: [PATCH] PID files contain the PID so that on next run, we can test to see if that PID is still alive. If it isn't, the daemon was kill -9'd, the system crashed, or similar. It isn't running any more and it's safe to start up again. Don't die on startup unless the PID in the PID file is valid. --- sbin/spectre.pl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/sbin/spectre.pl b/sbin/spectre.pl index 309467008..141eff668 100755 --- a/sbin/spectre.pl +++ b/sbin/spectre.pl @@ -114,8 +114,20 @@ elsif ($daemon) { die "Spectre is already running.\n"; } elsif (-e $pidFileName){ - die "pidFile $pidFileName already exists\n"; + # oh, ffs ... die "pidFile $pidFileName already exists\n"; + open my $pidFile, '<', $pidFileName or die "$pidFileName: $!"; + (my $pid) = readline $pidFile; + chomp $pid; + if(defined $pid and $pid =~ m/^(\d+)$/) { + if(kill 0, $1) { + die "$0: already running as PID $1"; + } else { + warn "pidfile contains $pid but that process seems to have terminated" + } + } + close $pidFile; } + # XXXX warn if we can't open the log file before forking or else make it not fatal or else close STDOUT/STDERR afterwards; don't fail silently -- sdw #fork and exit(sleep(1) and print((ping())?"Spectre failed to start!\n":"Spectre started successfully!\n")); #Can't have right now. require POSIX; fork and exit;