let daemons create pid files, so we can adopt them if they are still alive (makes...
[pxelator] / lib / PXElator / httpd.pm
index ae5827d..a2ba92f 100644 (file)
@@ -113,8 +113,22 @@ $SIG{CHLD} = 'IGNORE';
 
 sub start_stop {
        my $daemon = shift;
-       my $pid = $pids->{$daemon} || 'not started';
 
+       my $pid = $pids->{$daemon};
+       my $pid_path = "$server::conf/$daemon.pid";
+
+       if ( ! $pid && -e $pid_path ) {
+               my $p = read_file $pid_path;
+               if ( kill 0, $p ) {
+                       warn "adopted $daemon $p\n";
+                       return $pids->{$daemon} = $p;
+               } else {
+                       warn "old $daemon $p not running";
+                       unlink $pid_path;
+               }
+       }
+
+       $pid ||= 'not started';
        warn "start_stop $daemon $pid\n";
 
        if ( $pid =~ m{^\d+$} ) {
@@ -128,6 +142,7 @@ sub start_stop {
                if ( $pid = fork ) {
                        # parent
                        $pids->{$daemon} = $pid;
+                       write_file $pid_path, $pid;
                        warn "forked $daemon $pid\n";
                        return qq|$daemon pid $pid started|;
                } elsif ( defined $pid ) {