|
| 1 | +"""Generic linux daemon base class for python 3.x. |
| 2 | +
|
| 3 | +Source: https://web.archive.org/web/20160320091458/http://www.jejik.com/files/examples/daemon3x.py""" |
| 4 | + |
| 5 | +import sys |
| 6 | +import os |
| 7 | +import time |
| 8 | +import atexit |
| 9 | +import signal |
| 10 | + |
| 11 | + |
| 12 | +class Daemon: |
| 13 | + """A generic daemon class. |
| 14 | +
|
| 15 | + Usage: subclass the daemon class and override the run() method.""" |
| 16 | + |
| 17 | + def __init__(self, pidfile): |
| 18 | + self.pidfile = pidfile |
| 19 | + |
| 20 | + def daemonize(self): |
| 21 | + """Deamonize class. UNIX double fork mechanism.""" |
| 22 | + |
| 23 | + try: |
| 24 | + pid = os.fork() |
| 25 | + if pid > 0: |
| 26 | + # exit first parent |
| 27 | + sys.exit(0) |
| 28 | + except OSError as err: |
| 29 | + sys.stderr.write('fork #1 failed: {0}\n'.format(err)) |
| 30 | + sys.exit(1) |
| 31 | + |
| 32 | + # decouple from parent environment |
| 33 | + os.chdir('/') |
| 34 | + os.setsid() |
| 35 | + os.umask(0) |
| 36 | + |
| 37 | + # do second fork |
| 38 | + try: |
| 39 | + pid = os.fork() |
| 40 | + if pid > 0: |
| 41 | + |
| 42 | + # exit from second parent |
| 43 | + sys.exit(0) |
| 44 | + except OSError as err: |
| 45 | + sys.stderr.write('fork #2 failed: {0}\n'.format(err)) |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | + # redirect standard file descriptors |
| 49 | + sys.stdout.flush() |
| 50 | + sys.stderr.flush() |
| 51 | + si = open(os.devnull, 'r') |
| 52 | + so = open(os.devnull, 'a+') |
| 53 | + se = open(os.devnull, 'a+') |
| 54 | + |
| 55 | + os.dup2(si.fileno(), sys.stdin.fileno()) |
| 56 | + os.dup2(so.fileno(), sys.stdout.fileno()) |
| 57 | + os.dup2(se.fileno(), sys.stderr.fileno()) |
| 58 | + |
| 59 | + # write pidfile |
| 60 | + atexit.register(self.delpid) |
| 61 | + |
| 62 | + pid = str(os.getpid()) |
| 63 | + with open(self.pidfile, 'w+') as f: |
| 64 | + f.write(pid + '\n') |
| 65 | + |
| 66 | + def delpid(self): |
| 67 | + os.remove(self.pidfile) |
| 68 | + |
| 69 | + def start(self): |
| 70 | + """Start the daemon.""" |
| 71 | + |
| 72 | + # Check for a pidfile to see if the daemon already runs |
| 73 | + try: |
| 74 | + with open(self.pidfile, 'r') as pf: |
| 75 | + |
| 76 | + pid = int(pf.read().strip()) |
| 77 | + except IOError: |
| 78 | + pid = None |
| 79 | + |
| 80 | + if pid: |
| 81 | + message = "pidfile {0} already exist. " + \ |
| 82 | + "Daemon already running?\n" |
| 83 | + sys.stderr.write(message.format(self.pidfile)) |
| 84 | + sys.exit(1) |
| 85 | + |
| 86 | + # Start the daemon |
| 87 | + self.daemonize() |
| 88 | + self.run() |
| 89 | + |
| 90 | + def stop(self): |
| 91 | + """Stop the daemon.""" |
| 92 | + |
| 93 | + # Get the pid from the pidfile |
| 94 | + try: |
| 95 | + with open(self.pidfile, 'r') as pf: |
| 96 | + pid = int(pf.read().strip()) |
| 97 | + except IOError: |
| 98 | + pid = None |
| 99 | + |
| 100 | + if not pid: |
| 101 | + message = "pidfile {0} does not exist. " + \ |
| 102 | + "Daemon not running?\n" |
| 103 | + sys.stderr.write(message.format(self.pidfile)) |
| 104 | + return # not an error in a restart |
| 105 | + |
| 106 | + # Try killing the daemon process |
| 107 | + try: |
| 108 | + while 1: |
| 109 | + os.kill(pid, signal.SIGTERM) |
| 110 | + time.sleep(0.1) |
| 111 | + except OSError as err: |
| 112 | + e = str(err.args) |
| 113 | + if e.find("No such process") > 0: |
| 114 | + if os.path.exists(self.pidfile): |
| 115 | + os.remove(self.pidfile) |
| 116 | + else: |
| 117 | + print(str(err.args)) |
| 118 | + sys.exit(1) |
| 119 | + |
| 120 | + def restart(self): |
| 121 | + """Restart the daemon.""" |
| 122 | + self.stop() |
| 123 | + self.start() |
| 124 | + |
| 125 | + def run(self): |
| 126 | + """You should override this method when you subclass Daemon. |
| 127 | +
|
| 128 | + It will be called after the process has been daemonized by |
| 129 | + start() or restart().""" |
0 commit comments