From: "Anthony G. Basile" <blueness@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/grss:master commit in: grs/
Date: Fri, 10 Jul 2015 02:37:30 +0000 (UTC) [thread overview]
Message-ID: <1436496009.6e1ca591c2dacde806a277773d16b603558b04a2.blueness@gentoo> (raw)
commit: 6e1ca591c2dacde806a277773d16b603558b04a2
Author: Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Fri Jul 10 02:40:09 2015 +0000
Commit: Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Fri Jul 10 02:40:09 2015 +0000
URL: https://gitweb.gentoo.org/proj/grss.git/commit/?id=6e1ca591
grs/Daemon.py: document.
grs/Daemon.py | 37 ++++++++++++++++++++++++++++++-------
1 file changed, 30 insertions(+), 7 deletions(-)
diff --git a/grs/Daemon.py b/grs/Daemon.py
index 15290a5..0d2d988 100644
--- a/grs/Daemon.py
+++ b/grs/Daemon.py
@@ -7,22 +7,31 @@ import sys
import time
class Daemon:
- """ doc here
- more doc
- Adopted from Sander Marechal's "A simple unix/linux daemon in Python"
+ """ Adopted from Sander Marechal's "A simple unix/linux daemon in Python"
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
+
+ d = MyDaemon(pidfile, foo='1', bar='2') # Any number for kwargs after pidfile
+ d.start() # to start the daemon
+ d.restart() # to restart the daemon
+ d.stop() # to stop the daemon
+
+ 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
+ exceptions for debugging.
"""
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__.
+ """
self.pidfile = pidfile
for k in kwargs:
self.__dict__[k] = kwargs[k]
def daemonize(self):
- """ doc here
- more doc
- """
-
try:
pid = os.fork()
if pid > 0:
@@ -55,6 +64,9 @@ class Daemon:
sys.stderr.flush()
os.dup2(se.fileno(), sys.stderr.fileno())
+ # Use atexit to remove the pidfile when we shutdown.
+ # No matter where the exit is initiated, eg from Execute.py
+ # we are sure that atexit() will run and delete the pidfile.
atexit.register(self.delpid)
with open(self.pidfile,'w') as pf:
pf.write('%d\n' % os.getpid())
@@ -65,6 +77,10 @@ class Daemon:
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.
try:
with open(self.pidfile, 'r') as pf:
pid = int(pf.read().strip())
@@ -84,6 +100,13 @@ class Daemon:
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())
next reply other threads:[~2015-07-10 2:37 UTC|newest]
Thread overview: 139+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-07-10 2:37 Anthony G. Basile [this message]
-- strict thread matches above, loose matches on Subject: below --
2022-07-20 2:22 [gentoo-commits] proj/grss:master commit in: grs/ Anthony G. Basile
2022-07-20 1:49 Anthony G. Basile
2022-07-19 20:22 Anthony G. Basile
2022-07-19 20:18 Anthony G. Basile
2022-07-18 21:13 Anthony G. Basile
2019-04-19 12:29 Anthony G. Basile
2018-11-18 19:09 Anthony G. Basile
2018-11-12 3:08 Anthony G. Basile
2018-11-11 23:57 Anthony G. Basile
2018-11-11 23:35 Anthony G. Basile
2018-11-11 23:16 Anthony G. Basile
2018-06-25 20:32 Anthony G. Basile
2018-04-14 14:53 Anthony G. Basile
2018-04-10 3:14 Anthony G. Basile
2018-02-20 2:27 Anthony G. Basile
2018-02-20 2:04 Anthony G. Basile
2018-02-18 22:38 Anthony G. Basile
2018-02-18 16:37 Anthony G. Basile
2018-02-06 11:57 Anthony G. Basile
2018-02-06 11:42 Anthony G. Basile
2018-02-06 11:33 Anthony G. Basile
2018-02-06 10:55 Anthony G. Basile
2018-01-28 12:31 Anthony G. Basile
2018-01-28 12:09 Anthony G. Basile
2018-01-15 1:31 Anthony G. Basile
2018-01-15 1:23 Anthony G. Basile
2018-01-14 18:22 Anthony G. Basile
2018-01-14 17:05 Anthony G. Basile
2018-01-14 15:02 Anthony G. Basile
2018-01-14 14:37 Anthony G. Basile
2018-01-14 14:07 Anthony G. Basile
2018-01-13 17:40 Anthony G. Basile
2018-01-10 21:02 Anthony G. Basile
2018-01-10 20:40 Anthony G. Basile
2017-12-28 17:54 Anthony G. Basile
2017-12-28 17:22 Anthony G. Basile
2017-12-27 9:25 Anthony G. Basile
2017-12-26 14:19 Anthony G. Basile
2017-12-26 0:04 Anthony G. Basile
2017-12-26 0:04 Anthony G. Basile
2017-12-26 0:04 Anthony G. Basile
2017-12-25 23:38 Anthony G. Basile
2017-12-25 23:24 Anthony G. Basile
2017-12-23 16:04 Anthony G. Basile
2017-12-23 14:15 Anthony G. Basile
2016-03-23 15:08 Anthony G. Basile
2016-03-23 15:05 Anthony G. Basile
2015-11-26 19:48 Anthony G. Basile
2015-10-10 20:20 Anthony G. Basile
2015-10-10 19:36 Anthony G. Basile
2015-10-10 19:07 Anthony G. Basile
2015-10-10 12:22 Anthony G. Basile
2015-10-10 11:44 Anthony G. Basile
2015-10-10 11:22 Anthony G. Basile
2015-10-10 11:20 Anthony G. Basile
2015-10-10 11:10 Anthony G. Basile
2015-10-10 1:46 Anthony G. Basile
2015-10-10 1:40 Anthony G. Basile
2015-10-10 1:34 Anthony G. Basile
2015-10-10 1:26 Anthony G. Basile
2015-10-10 1:23 Anthony G. Basile
2015-10-10 1:18 Anthony G. Basile
2015-10-10 0:34 Anthony G. Basile
2015-10-10 0:29 Anthony G. Basile
2015-10-09 23:33 Anthony G. Basile
2015-10-09 23:33 Anthony G. Basile
2015-10-09 23:30 Anthony G. Basile
2015-10-09 23:11 Anthony G. Basile
2015-10-09 23:06 Anthony G. Basile
2015-10-08 23:52 Anthony G. Basile
2015-10-08 18:03 Anthony G. Basile
2015-10-08 17:20 Anthony G. Basile
2015-10-07 11:28 Anthony G. Basile
2015-10-07 11:15 Anthony G. Basile
2015-10-07 11:07 Anthony G. Basile
2015-10-07 11:03 Anthony G. Basile
2015-10-07 10:57 Anthony G. Basile
2015-10-07 8:31 Anthony G. Basile
2015-10-07 8:01 Anthony G. Basile
2015-10-06 10:07 Anthony G. Basile
2015-09-16 22:08 Anthony G. Basile
2015-09-16 5:16 Anthony G. Basile
2015-09-16 0:37 Anthony G. Basile
2015-09-16 0:32 Anthony G. Basile
2015-09-14 7:17 Anthony G. Basile
2015-09-14 5:32 Anthony G. Basile
2015-09-14 5:25 Anthony G. Basile
2015-09-14 0:29 Anthony G. Basile
2015-09-13 17:31 Anthony G. Basile
2015-09-12 20:56 Anthony G. Basile
2015-09-12 20:56 Anthony G. Basile
2015-08-09 20:26 Anthony G. Basile
2015-08-08 20:06 Anthony G. Basile
2015-08-08 18:01 Anthony G. Basile
2015-08-08 17:51 Anthony G. Basile
2015-07-29 0:20 Anthony G. Basile
2015-07-28 22:44 Anthony G. Basile
2015-07-27 20:49 Anthony G. Basile
2015-07-25 22:36 Anthony G. Basile
2015-07-14 23:37 Anthony G. Basile
2015-07-14 23:29 Anthony G. Basile
2015-07-14 23:19 Anthony G. Basile
2015-07-14 23:16 Anthony G. Basile
2015-07-14 23:02 Anthony G. Basile
2015-07-14 22:39 Anthony G. Basile
2015-07-14 22:35 Anthony G. Basile
2015-07-14 21:45 Anthony G. Basile
2015-07-14 21:21 Anthony G. Basile
2015-07-14 21:02 Anthony G. Basile
2015-07-14 20:55 Anthony G. Basile
2015-07-10 12:54 Anthony G. Basile
2015-07-10 2:37 Anthony G. Basile
2015-07-10 2:01 Anthony G. Basile
2015-07-09 15:26 Anthony G. Basile
2015-07-09 1:44 Anthony G. Basile
2015-07-08 15:44 Anthony G. Basile
2015-07-08 10:28 Anthony G. Basile
2015-07-07 20:52 Anthony G. Basile
2015-07-07 20:34 Anthony G. Basile
2015-07-07 14:30 Anthony G. Basile
2015-07-07 12:54 Anthony G. Basile
2015-07-07 12:46 Anthony G. Basile
2015-07-07 12:44 Anthony G. Basile
2015-07-07 12:33 Anthony G. Basile
2015-07-07 11:26 Anthony G. Basile
2015-07-07 2:17 Anthony G. Basile
2015-07-06 20:13 Anthony G. Basile
2015-07-06 18:13 Anthony G. Basile
2015-07-05 16:49 Anthony G. Basile
2015-07-05 12:04 Anthony G. Basile
2015-07-02 22:30 Anthony G. Basile
2015-07-01 17:27 Anthony G. Basile
2015-07-01 17:03 Anthony G. Basile
2015-07-01 16:53 Anthony G. Basile
2015-07-01 16:52 Anthony G. Basile
2015-07-01 16:07 Anthony G. Basile
2015-07-01 12:30 Anthony G. Basile
2015-07-01 12:23 Anthony G. Basile
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1436496009.6e1ca591c2dacde806a277773d16b603558b04a2.blueness@gentoo \
--to=blueness@gentoo.org \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox