aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony G. Basile <blueness@gentoo.org>2015-07-10 15:09:48 -0400
committerAnthony G. Basile <blueness@gentoo.org>2015-07-10 15:09:48 -0400
commit3905cabb9652305dcd220b96bb53d8d1617d30ca (patch)
tree486f5d764f4e349f3789fcad604195d15416334f /grs/Daemon.py
parentgrs/Interpret.py: more aggressively send TERM/KILL signals. (diff)
downloadgrss-3905cabb9652305dcd220b96bb53d8d1617d30ca.tar.gz
grss-3905cabb9652305dcd220b96bb53d8d1617d30ca.tar.bz2
grss-3905cabb9652305dcd220b96bb53d8d1617d30ca.zip
grs/Daemon.py: remove unused stop() and restart().
Diffstat (limited to 'grs/Daemon.py')
-rw-r--r--grs/Daemon.py73
1 files changed, 14 insertions, 59 deletions
diff --git a/grs/Daemon.py b/grs/Daemon.py
index 0d2d988..38b7d9a 100644
--- a/grs/Daemon.py
+++ b/grs/Daemon.py
@@ -11,12 +11,12 @@ class Daemon:
See: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
To use, inherit by a subclass which overrides run() and does all the
- daemon work. You start the daemon with
+ work. You start the daemon with
- d = MyDaemon(pidfile, foo='1', bar='2') # Any number for kwargs after pidfile
+ d = MyDaemon(pidfile, foo='1', bar='2') # Any **kwargs after pidfile
d.start() # to start the daemon
- d.restart() # to restart the daemon
- d.stop() # to stop the daemon
+
+ All signal handling should be defined in a subfunction within run().
Note: This isn't completely general daemon code as it doesn't close stdout/stderr.
Rather these are redirected to /var/log/grs/grs-daemon-<pid>.err to capture any
@@ -24,13 +24,13 @@ class Daemon:
"""
def __init__(self, pidfile, **kwargs):
- """ Since this will be used as a super class, we'll accept any **kwargs
- and insert them to our internal __dict__.
- """
+ # Since this will be used as a super class, we'll accept any **kwargs
+ # and insert them to our internal __dict__.
self.pidfile = pidfile
for k in kwargs:
self.__dict__[k] = kwargs[k]
+
def daemonize(self):
try:
pid = os.fork()
@@ -76,11 +76,14 @@ class Daemon:
os.remove(self.pidfile)
+ def run(self):
+ pass
+
+
def start(self):
- # If there's a pidfile when we try to startup, then either
- # its stale or we're already running. If the pidfile is stale,
- # remove it and startup as usual. If we're already running,
- # then don't start a second instance.
+ # If there's a pidfile when we try to startup, then:
+ # 1) If the pidfile is stale, remove it and startup as usual.
+ # 2) If we're already running, then don't start a second instance.
try:
with open(self.pidfile, 'r') as pf:
pid = int(pf.read().strip())
@@ -97,51 +100,3 @@ class Daemon:
self.daemonize()
self.run()
-
-
- def stop(self):
- # Try to open our pidfile and read our pid. If you have a pid but
- # there is no process at that pid, then we're not running and all
- # we have to do is cleanup our stale pidfile.a If we can't get a
- # pid from our pidfile, then we've lost the original process. Either
- # it crashed or something else killed the pidfile. We don't know.
- # Finally if have a valid pid, send it a bunch of SIGTERMS followed
- # by SIGKILLS just in case.
- try:
- with open(self.pidfile,'r') as pf:
- pid = int(pf.read().strip())
- except IOError:
- pid = None
-
- if pid and not os.path.exists('/proc/%d' % pid):
- sys.stderr.write('process not running\n')
- sys.stderr.write('unlinking stale pid file %s\n' % self.pidfile)
- os.unlink(self.pidfile)
- return
-
- if not pid:
- sys.stderr.write('process not running\n')
- return # not an error in a restart
-
- try:
- for i in range(10):
- os.kill(pid, signal.SIGTERM)
- time.sleep(0.2)
- while True:
- os.kill(pid, signal.SIGKILL)
- time.sleep(0.2)
- except ProcessLookupError as err:
- try:
- os.remove(self.pidfile)
- except IOError as err:
- sys.stderr.write('%s\n' % err)
- except OSError as err:
- sys.stderr.write('%s\n' %err)
- return
-
- def restart(self):
- self.stop()
- self.start()
-
- def run(self):
- pass