Skip to content

Commit 83d5f55

Browse files
committed
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.
1 parent 3341153 commit 83d5f55

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

sbin/spectre.pl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,20 @@ BEGIN
114114
die "Spectre is already running.\n";
115115
}
116116
elsif (-e $pidFileName){
117-
die "pidFile $pidFileName already exists\n";
117+
# oh, ffs ... die "pidFile $pidFileName already exists\n";
118+
open my $pidFile, '<', $pidFileName or die "$pidFileName: $!";
119+
(my $pid) = readline $pidFile;
120+
chomp $pid;
121+
if(defined $pid and $pid =~ m/^(\d+)$/) {
122+
if(kill 0, $1) {
123+
die "$0: already running as PID $1";
124+
} else {
125+
warn "pidfile contains $pid but that process seems to have terminated"
126+
}
127+
}
128+
close $pidFile;
118129
}
130+
# 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
119131
#fork and exit(sleep(1) and print((ping())?"Spectre failed to start!\n":"Spectre started successfully!\n")); #Can't have right now.
120132
require POSIX;
121133
fork and exit;

0 commit comments

Comments
 (0)