]> Frank Brehm's Git Trees - my-stuff/postfix.git/commitdiff
PID-File Behandlung dazu
authorFrank Brehm <frank@brehm-online.com>
Mon, 3 Nov 2008 21:39:46 +0000 (21:39 +0000)
committerFrank Brehm <frank@brehm-online.com>
Mon, 3 Nov 2008 21:39:46 +0000 (21:39 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/postfix@22 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

get-lookup-tables.pl

index eac5a8640d47d1a1553ff0fc3c7733ebb77c0a19..8bdf0459987ff4276c895e1ea5491dad9238bed2 100755 (executable)
@@ -23,6 +23,16 @@ $basename       =~ s/\.pl$//i;
 my $postfix_dir = abs_path( catfile( '', 'etc', 'postfix' ) );
 my $cfg_file    = abs_path( catfile( $postfix_dir, $basename . ".conf" ) );
 
+my $pidfile = '/var/run/get-lookup-tables.pid';
+my $pidfile_written = undef;
+
+END {
+    if ( -f $pidfile and $pidfile_written ) {
+        debug( 2, "Lösche PID-Datei '" . $pidfile . "' ..." );
+        unlink $pidfile;
+    }
+}
+
 GetOptions(
     "verbose+"              => \$verbose,
     "conf-file|conf|c=s"    => \$cfg_file,
@@ -34,9 +44,53 @@ unless ( -d $postfix_dir ) {
 
 fatal( "Datei '$cfg_file' existiert nicht." ) unless -f $cfg_file;
 my $config = read_config($cfg_file);
+check_pidfile();
+
+
+#--------------------------------------------------------------------------------------
 
+=head2 check_pidfile( )
 
+=cut
 
+sub check_pidfile {
+
+    if ( -f $pidfile ) {
+        unless ( open PIDFILE, "<", $pidfile ) {
+            fatal( "Konnte PID-Datei '" . $pidfile . "' nicht lesen: ", $! );
+        }
+        my $pid = undef;
+        while ( my $line = <PIDFILE> ) {
+            if ( $line =~ /(\d+)/ ) {
+                $pid = $1;
+                last;
+            }
+        }
+        close PIDFILE;
+        if ( $pid ) {
+            debug( 2, "Suche nach Prozess Nr. " . $pid . " ..." );
+            if ( kill 0, $pid ) {
+                fatal( "Es läuft noch ein " . $basename . "-Prozess mit der PID $pid" );
+            }
+            else {
+                notice( "Prozess mit der PID $pid unbekannterweise verstorben." );
+            }
+        }
+        else {
+            notice( "Konnte PID-Datei '" . $pidfile . "' nicht auswerten." );
+        }
+    }
+
+    debug( 2, "Schreibe PID-Datei '" . $pidfile . "' ..." );
+    unless ( open PIDFILE, ">", $pidfile ) {
+        fatal( "Konnte PID-Datei '" . $pidfile . "'  nicht zum Schreiben öffnen: ", $! );
+    }
+    print PIDFILE $$;
+    print PIDFILE "\n";
+    close PIDFILE;
+    $pidfile_written = 1;
+
+}
 
 #--------------------------------------------------------------------------------------
 
@@ -63,6 +117,8 @@ sub read_config {
         fatal( "Fehler beim Lesen der Konfigurationsdatei '" . $cfg_file . "': ", $@ );
     }
 
+    $pidfile = $config->{'pid_file'} if $config->{'pid_file'};
+
     debug( 2, "Gelesene Konfiguration: ", $config );
 
     return $config;