if not self._check_pidfile():
sys.exit(3)
+ if not self._write_pidfile():
+ sys.exit(3)
+
self.logger.debug( _("Logrotating ready for work") )
#------------------------------------------------------------
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:
@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()
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__":
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)