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