1 # -*- coding: utf-8 -*-
4 # Copyright 2007, 2009 Sander Marechal <s.marechal@jejik.com>
5 # http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
7 # Copyright 2010, 2011 kaliko <kaliko@azylum.org>
8 # https://gitorious.org/python-daemon
10 # This file is part of MPD_sima
12 # MPD_sima is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
17 # MPD_sima is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with MPD_sima. If not, see <http://www.gnu.org/licenses/>.
31 from signal import signal, SIGTERM, SIGHUP, SIGUSR1
36 A generic daemon class.
38 Usage: subclass the Daemon class and override the run() method
40 Daemon([pidfile[, stdin[, stdout[, stderr]]]])
42 pidfile : file to write pid to (default no pid file writen)
43 stdin : standard input file descriptor (default to /dev/null)
44 stdout : standard output file descriptor (default to /dev/null)
45 stderr : standard error file descriptorr (default to /dev/null)
49 def __init__(self, pidfile,
56 self.pidfile = pidfile
61 Do the UNIX double-fork magic.
62 see W. Richard Stevens, "Advanced Programming in the Unix Environment"
63 for details (ISBN 0201563177)
66 Unix processes belong to "process group" which in turn lies within a
67 "session". A session can have a controlling tty.
68 Forking twice allows to detach the session from a possible tty.
69 The process lives then within the init process.
77 sys.stderr.write('fork #1 failed: {0.errno:d} ({0.strerror})\n'.format(e))
80 # Decouple from parent environment
83 self.umask = os.umask(0)
89 # exit from second parent
92 sys.stderr.write('fork #2 failed: {0.errno:d} ({0.strerror})\n'.format(e))
96 # redirect standard file descriptors
99 # TODO: binary or txt mode?
100 si = open(self.stdin, mode='rb')
101 so = open(self.stdout, mode='ab+')
102 se = open(self.stderr, mode='ab+', buffering=0)
103 os.dup2(si.fileno(), sys.stdin.fileno())
104 os.dup2(so.fileno(), sys.stdout.fileno())
105 os.dup2(se.fileno(), sys.stderr.fileno())
107 atexit.register(self.shutdown)
108 self.signal_management()
114 pid = str(os.getpid())
117 open(self.pidfile, 'w').write('%s\n' % pid)
118 except Exception as wpid_err:
119 sys.stderr.write('Error trying to write pid file: {}\n'.format(wpid_err))
122 atexit.register(self.delpid)
124 def signal_management(self):
125 """Declare signal handlers
127 signal(SIGTERM, self.exit_handler)
128 signal(SIGHUP, self.hup_handler)
129 signal(SIGUSR1, self.hup_handler)
131 def exit_handler(self, signum, frame):
134 def hup_handler(self, signum, frame):
139 """Remove PID file"""
141 os.unlink(self.pidfile)
142 except OSError as err:
143 message = 'Error trying to remove PID file: {}\n'
144 sys.stderr.write(message.format(err))
150 # Check for a pidfile to see if the daemon already runs
152 pf = open(self.pidfile, 'r')
153 pid = int(pf.read().strip())
159 message = 'pidfile {0.pidfile} already exist. Daemon already running?\n'
160 sys.stderr.write(message.format(self))
167 def foreground(self):
169 Foreground/debug mode
172 atexit.register(self.shutdown)
179 # Get the pid from the pidfile
181 pf = open(self.pidfile, 'r')
182 pid = int(pf.read().strip())
188 message = 'pidfile {0.pidfile} does not exist. Is the Daemon running?\n'
189 sys.stderr.write(message.format(self))
190 return # not an error in a restart
192 # Try killing the daemon process
194 os.kill(pid, SIGTERM)
196 except OSError as err:
198 if os.path.exists(self.pidfile):
199 message = "Daemon's not running? removing pid file {0.pidfile}.\n"
200 sys.stderr.write(message.format(self))
201 os.remove(self.pidfile)
203 sys.stderr.write(err.strerror)
215 You should override this method when you subclass Daemon. It will be
216 called when the process is being stopped.
218 Daemon() uses atexit to call Daemon().shutdown(), as a consequence
219 shutdown and any other functions registered via this module are not
220 called when the program is killed by an un-handled/unknown signal.
221 This is the reason of Daemon().signal_management() existence.
226 You should override this method when you subclass Daemon. It will be
227 called after the process has been daemonized by start() or restart().
231 # vim: ai ts=4 sw=4 sts=4 expandtab