Skip to content
Browse files

added README.md, minor code cleanup and refactoring, updated versioning

  • Loading branch information...
1 parent f6629d6 commit 4b8510da90d692569b512f00b8b9e36beb78eb0f @fredrick fredrick committed Nov 22, 2010
Showing with 89 additions and 40 deletions.
  1. +57 −0 README.md
  2. +3 −7 setup.py
  3. +17 −22 src/2.x.x/daemon.py
  4. +12 −11 src/3.x.x/daemon.py
View
57 README.md
@@ -0,0 +1,57 @@
+#Daemon-Python (0.2)
+Lightweight and no-nonsense POSIX daemon library. Extensible, complete with process forking and [PID][4] management.
+
+Inspired by [Sander Marechal][1].
+
+##License
+MIT/X11 - See [LICENSE][2]
+
+##Getting Started
+1. **Installation**
+
+ # git clone https://github.com/stackd/daemon-py.git
+ # cd seat-py/
+ # python setup.py install
+
+ *Note: Daemon-Python, as of version 0.2, has only been tested on Linux. The setup.py script will automatically detect if the underlying platform is compatible, as well as determine the appropriate version of Python.*
+
+2. **Instantiation**
+
+ import daemon
+ class MyDaemon(daemon.Daemon):
+ """Subclass Daemon-Python."""
+ ...
+ def run(self):
+ """Define what to daemonize by implementing run() method."""
+ ...
+
+3. **Implementing control**
+
+ Finally, we want to be able to control our daemon.
+
+ ...
+ if __name__ == "__main__":
+ daemon = MyDaemon('/tmp/mydaemon.pid') #Define a pidfile location (typically located in /tmp or /var/run)
+ if len(sys.argv) == 2:
+ if 'start' == sys.argv[1]:
+ daemon.start()
+ elif 'stop' == sys.argv[1]:
+ daemon.stop()
+ elif 'restart' == sys.argv[1]:
+ daemon.restart()
+ elif 'status' == sys.argv[1]:
+ daemon.status()
+ else:
+ sys.stdout.write("Unknown command\n")
+ sys.exit(2)
+ sys.exit(0)
+ else:
+ sys.stdout.write("Usage: %s start|stop|restart|status\n" % sys.argv[0])
+ sys.exit(2)
+
+For a complete basic implementation, see [src/test.py][3]
+
+ [1]: https://github.com/stackd/daemon-py/blob/master/LICENSE
+ [2]: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
+ [3]: https://github.com/stackd/daemon-py/blob/master/src/test.py
+ [4]: http://en.wikipedia.org/wiki/Process_identifier
View
10 setup.py
@@ -4,25 +4,21 @@
from distutils.core import setup
from distutils.util import get_platform
-"""
-Verify Python platform is Linux
-"""
+"""Verify Python platform is Linux."""
platform = get_platform()
if platform.startswith('linux') == False:
sys.stderr.write("Daemon-Python is not compatible with %s\n" % platform)
sys.exit(1)
-"""
-Determine appropriate Python version for installation
-"""
+"""Determine appropriate Python version for installation."""
if sys.version_info[0] >= 3:
package_dir = {'': 'src/3.x.x'}
else:
package_dir = {'': 'src/2.x.x'}
setup(
name = 'daemon',
- version = '0.1',
+ version = '0.2',
description = 'Lightweight and no-nonsense POSIX daemon library for Python (2.x.x/3.x.x)',
author = 'Fredrick Galoso - Stackd, LLC',
license = 'MIT/X11',
View
39 src/2.x.x/daemon.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
"""
-Daemon-Python (0.1)
+Daemon-Python (0.2)
Lightweight and no-nonsense POSIX daemon library
https://github.com/stackd/daemon-py
@@ -24,8 +24,7 @@
from signal import SIGTERM
class Daemon(object):
- """
- A generic daemon class for Python 2.x.x
+ """ A generic daemon class for Python 2.x.x
Usage: subclass the Daemon class and override the run() method
"""
@@ -36,10 +35,11 @@ def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/
self.pidfile = pidfile
def daemonize(self):
+ """UNIX double fork mechanism.
+
+ See Stevens' "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177).
"""
- UNIX double fork mechanism, see Stevens' "Advanced
- Programming in the UNIX Environment" for details (ISBN 0201563177)
- """
+
try:
pid = os.fork()
if pid > 0:
@@ -83,10 +83,9 @@ def delpid(self):
os.remove(self.pidfile)
def start(self):
- """
- Start the daemon
- """
- # Check for a pidfile to see if the daemon is already running
+ """Start the daemon."""
+
+ #Check for a pidfile to see if the daemon is already running.
try:
pf = file(self.pidfile,'r')
pid = int(pf.read().strip())
@@ -104,10 +103,9 @@ def start(self):
self.run()
def stop(self):
- """
- Stop the daemon
- """
- # Get the pid from the pidfile
+ """Stop the daemon."""
+
+ #Get the pid from the pidfile.
try:
pf = file(self.pidfile,'r')
pid = int(pf.read().strip())
@@ -135,16 +133,14 @@ def stop(self):
sys.exit(1)
def restart(self):
- """
- Restart the daemon
- """
+ """Restart the daemon."""
+
self.stop()
self.start()
def status(self):
- """
- Return the daemon state.
- """
+ """Return the daemon state."""
+
# Check for a pidfile to see if the daemon is already running
try:
pf = file(self.pidfile,'r')
@@ -161,8 +157,7 @@ def status(self):
sys.stdout.write(message % (self.pidfile, self.__class__.__name__))
def run(self):
- """
- You should override this method when you subclass Daemon.
+ """Override this method when you subclass Daemon.
It will be called after the process has been daemonized by
start() or restart().
View
23 src/3.x.x/daemon.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
"""
-Daemon-Python (0.1)
+Daemon-Python (0.2)
Lightweight and no-nonsense POSIX daemon library
https://github.com/stackd/daemon-py
@@ -24,15 +24,17 @@
import signal
class Daemon:
- """
- A generic daemon class for Python 3.x.x
-
- Usage: subclass the daemon class and override the run() method.
+ """ A generic daemon class for Python 3.x.x
+
+ Usage: subclass the Daemon class and override the run() method
"""
def __init__(self, pidfile): self.pidfile = pidfile
def daemonize(self):
- """Deamonize class. UNIX double fork mechanism."""
+ """UNIX double fork mechanism.
+
+ See Stevens' "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177).
+ """
try:
pid = os.fork()
@@ -133,13 +135,13 @@ def stop(self):
def restart(self):
"""Restart the daemon."""
+
self.stop()
self.start()
def status(self):
- """
- Return the daemon state.
- """
+ """Return the daemon state."""
+
# Check for a pidfile to see if the daemon is already running
try:
with open(self.pidfile,'r') as pf:
@@ -156,8 +158,7 @@ def status(self):
sys.stdout.write(message.format(self.pidfile, self.__class__.__name__))
def run(self):
- """
- You should override this method when you subclass Daemon.
+ """Override this method when you subclass Daemon.
It will be called after the process has been daemonized by
start() or restart().

0 comments on commit 4b8510d

Please sign in to comment.
Something went wrong with that request. Please try again.