X-Git-Url: https://git.kaliko.me/?p=python-daemon.git;a=blobdiff_plain;f=src%2Fdaemon.py;h=2251f183246a3e23171aeb2412eaf07035f70f04;hp=787cb65681ae720fbfe48e0d5e8da86239d52c19;hb=06a8ed720fbf9aa31a5d8f093c102239952aa6c0;hpb=5ccfc6d3e18ead1c13109af66befcd7a56d85d93 diff --git a/src/daemon.py b/src/daemon.py index 787cb65..2251f18 100644 --- a/src/daemon.py +++ b/src/daemon.py @@ -20,6 +20,8 @@ class Daemon(object): Usage: subclass the Daemon class and override the run() method """ + version = "0.3" + def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', @@ -28,6 +30,7 @@ class Daemon(object): self.stdout = stdout self.stderr = stderr self.pidfile = pidfile + self.umask = 0 def daemonize(self): """ @@ -47,7 +50,7 @@ class Daemon(object): # Decouple from parent environment os.chdir("/") os.setsid() - os.umask(0) + self.umask = os.umask(0) # Do second fork try: @@ -79,12 +82,14 @@ class Daemon(object): return pid = str(os.getpid()) try: + os.umask(self.umask) file(self.pidfile, 'w').write('%s\n' % pid) #except IOError, wpid_err: except Exception, wpid_err: sys.stderr.write(u'Error trying to write pid file to %s: %s\n' % (unicode(self.pidfile, 'utf-8'), wpid_err)) sys.exit(1) + os.umask(0) atexit.register(self.delpid) def signal_management(self): @@ -95,7 +100,7 @@ class Daemon(object): sys.exit(1) def delpid(self): - os.remove(self.pidfile) + os.unlink(self.pidfile) def start(self): """ @@ -139,7 +144,7 @@ class Daemon(object): pid = None if not pid: - message = "pidfile %s does not exist. Daemon not running?\n" + message = "pidfile %s does not exist. Is the Daemon running?\n" sys.stderr.write(message % self.pidfile) return # not an error in a restart @@ -148,10 +153,9 @@ class Daemon(object): os.kill(pid, SIGTERM) time.sleep(0.1) except OSError, err: - err = str(err) - if err.find("No such process") > 0: + if err.errno == 3: if os.path.exists(self.pidfile): - message = "Daemon not running? removing pid file %s.\n" + message = "Daemon's not running? removing pid file %s.\n" sys.stderr.write(message % self.pidfile) os.remove(self.pidfile) else: