X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=vstf%2Fvstf%2Fcommon%2Fdaemon.py;h=35933dad7e69640db12ab8319d2f49aa8221a552;hb=194ae04e266f63512a8f041f6bfd0dc330cd81af;hp=1085d36ce5981e44aa94903b4892ffd377d66fed;hpb=830b8c8c9f158fad8f9b1fc0977ae6deee3e9bed;p=bottlenecks.git diff --git a/vstf/vstf/common/daemon.py b/vstf/vstf/common/daemon.py index 1085d36c..35933dad 100755 --- a/vstf/vstf/common/daemon.py +++ b/vstf/vstf/common/daemon.py @@ -20,45 +20,46 @@ class Daemon(object): Usage: subclass the Daemon class and override the run() method """ + def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): super(Daemon, self).__init__() self.stdin = stdin self.stdout = stdout self.stderr = stderr self.pidfile = pidfile - + def daemonize(self): """ do the UNIX double-fork magic, see Stevens' "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177) http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 """ - try: - pid = os.fork() + try: + pid = os.fork() if pid > 0: - sys.exit(0) - except OSError, e: + sys.exit(0) + except OSError, e: LOG.error("fork #1 failed: %(errno)s, %(strerror)s", - {'errno':e.errno, 'strerror': e.strerror}) + {'errno': e.errno, 'strerror': e.strerror}) sys.exit(1) - + # decouple from parent environment - os.chdir("/") - os.setsid() - os.umask(0) - + os.chdir("/") + os.setsid() + os.umask(0) + # do second fork - try: - pid = os.fork() + try: + pid = os.fork() if pid > 0: # exit from second parent - sys.exit(0) - except OSError, e: + sys.exit(0) + except OSError, e: LOG.error("fork #1 failed: %(errno)s, %(strerror)s", - {'errno':e.errno, 'strerror': e.strerror}) - sys.exit(1) - - # redirect standard file descriptors + {'errno': e.errno, 'strerror': e.strerror}) + sys.exit(1) + + # redirect standard file descriptors sys.stdout.flush() sys.stderr.flush() si = file(self.stdin, 'r') @@ -67,12 +68,12 @@ class Daemon(object): os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) - + # write pidfile atexit.register(self.delpid) pid = str(os.getpid()) - file(self.pidfile,'w+').write("%s\n" % pid) - + file(self.pidfile, 'w+').write("%s\n" % pid) + def delpid(self): os.remove(self.pidfile) @@ -80,15 +81,15 @@ class Daemon(object): """ Start the daemon """ - + # Check for a pidfile to see if the daemon already runs try: - pf = file(self.pidfile,'r') + pf = file(self.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None - + if pid: message = "pidfile %s already exist. Daemon already running?\n" sys.stderr.write(message % self.pidfile) @@ -104,16 +105,16 @@ class Daemon(object): """ # Get the pid from the pidfile try: - pf = file(self.pidfile,'r') + pf = file(self.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None - + if not pid: message = "pidfile %s does not exist. Daemon not running?\n" sys.stderr.write(message % self.pidfile) - return # not an error in a restart + return # not an error in a restart # Try killing the daemon process try: @@ -144,10 +145,10 @@ class Daemon(object): """ pass - + def daemon_die(self): - """You should this method when you shutdown daemon + """You should override this method when you shutdown daemon this func will be call by stop() before kill the process """ - pass \ No newline at end of file + pass