]> Frank Brehm's Git Trees - my-stuff/py-logrotate.git/commitdiff
PID-Datei-Behandlung abgeschlossen ...
authorFrank Brehm <frank@brehm-online.com>
Mon, 23 May 2011 21:02:49 +0000 (21:02 +0000)
committerFrank Brehm <frank@brehm-online.com>
Mon, 23 May 2011 21:02:49 +0000 (21:02 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/PyLogrotate/trunk@247 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

LogRotateHandler.py
logrotate.py
test/apache2

index 9ec7c2bb44820a1678d462e581988cac09c6c358..5b6bde5357786d2d26954193cf0db8c8c2c33cd6 100755 (executable)
@@ -215,6 +215,9 @@ class LogrotateHandler(object):
         if not self._check_pidfile():
             sys.exit(3)
 
+        if not self._write_pidfile():
+            sys.exit(3)
+
         self.logger.debug( _("Logrotating ready for work") )
 
     #------------------------------------------------------------
@@ -254,8 +257,11 @@ class LogrotateHandler(object):
         No parameters, no return value.
         '''
 
+        _ = self.t.lgettext
+
         if self.pidfile_created:
             if os.path.exists(self.pid_file):
+                self.logger.debug( _("Removing PID file '%s' ...") % (self.pid_file) )
                 try:
                     os.remove(self.pid_file)
                 except OSError, e:
@@ -324,14 +330,27 @@ class LogrotateHandler(object):
         @rtype:  bool
         '''
 
+        _ = self.t.lgettext
+
         if not os.path.exists(self.pid_file):
+            if self.verbose > 1:
+                self.logger.debug( _("PID file '%s' doesn't exists.") % (self.pid_file) )
             return True
 
         if self.test:
             self.logger.info( _("Testmode, skip test of PID file '%s'.") % (self.pid_file) )
             return True
 
-        f = open(self.pid_file, 'r')
+        self.logger.debug( _("Reading PID file '%s' ...") % (self.pid_file) )
+        f = None
+        try:
+            f = open(self.pid_file, 'r')
+        except IOError, e:
+            raise LogrotateHandlerError(
+                _("Couldn't open PID file '%(file)s' for reading: %(msg)s")
+                % { 'file': self.pid_file, 'msg': str(e) }
+            )
+
         line = f.readline()
         f.close()
 
@@ -366,6 +385,43 @@ class LogrotateHandler(object):
 
         return False
 
+    #------------------------------------------------------------
+    def _write_pidfile(self):
+        '''
+        Writes the PID of the current process in self.pid_file.
+
+        Exit with an error, if it's not possible to write.
+        Doesn't exit in test mode.
+
+        Writes on success (no other process) this PID file.
+
+        @return: Success
+        @rtype:  bool
+        '''
+
+        _ = self.t.lgettext
+
+        if self.test:
+            self.logger.info( _("Testmode, skip writing of PID file '%s'.") % (self.pid_file) )
+            return True
+
+        self.logger.info( _("Writing PID file '%s' ...") % (self.pid_file) )
+
+        f = None
+        try:
+            f = open(self.pid_file, 'w')
+            f.write(str(os.getppid()) + "\n")
+            f.close()
+        except IOError, e:
+            raise LogrotateHandlerError(
+                _("Couldn't open PID file '%(file)s' for writing: %(msg)s")
+                % { 'file': self.pid_file, 'msg': str(e) }
+            )
+
+        self.pidfile_created = True
+
+        return True
+
 #========================================================================
 
 if __name__ == "__main__":
index 71c3f29c6db36d22f402d5e967249500d2733342..1d405353f706060238e67a4ec96c7ca1592f2604 100755 (executable)
@@ -78,16 +78,21 @@ def main():
         if verbose_level < 1:
             verbose_level = 1
 
-    lr_handler = LogrotateHandler(
-        opt_parser.args[0],
-        test       = testmode,
-        verbose    = verbose_level,
-        force      = opt_parser.options.force,
-        state_file = opt_parser.options.statefile,
-        pid_file   = opt_parser.options.pidfile,
-        mail_cmd   = opt_parser.options.mailcmd,
-        local_dir  = local_dir,
-    )
+    lr_handler = None
+    try:
+        lr_handler = LogrotateHandler(
+            opt_parser.args[0],
+            test       = testmode,
+            verbose    = verbose_level,
+            force      = opt_parser.options.force,
+            state_file = opt_parser.options.statefile,
+            pid_file   = opt_parser.options.pidfile,
+            mail_cmd   = opt_parser.options.mailcmd,
+            local_dir  = local_dir,
+        )
+    except LogrotateHandlerError, e:
+        sys.stderr.write(str(e) + "\n")
+        sys.exit(9)
 
     if opt_parser.options.verbose > 2:
         print _("Handler object structure") + ':\n' + str(lr_handler)
index a874b3d7f69b43310bc8ea62646c636f47548355..e90753e104b272b289a7b4a94652fcb4c5140a05 100644 (file)
@@ -2,6 +2,7 @@
 # Contributes by Chuck Short
 #
 
+pidfile /home/frank/Development/Python/PyLogrotate/logrotate.pid
 
 /var/log/apache2/access_log
 /var/log/apache2/*.log