Daemonize and pid_file support

This commit is contained in:
Ruud
2012-01-15 02:03:30 +01:00
parent 01f3a68812
commit 3ad0f91dc4
3 changed files with 135 additions and 171 deletions
+14 -5
View File
@@ -1,7 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*-
'''Wrapper for the command line interface.'''
from os.path import dirname from os.path import dirname
import os import os
import sys import sys
@@ -44,8 +41,7 @@ def start():
return 0 return 0
from couchpotato.runner import runCouchPotato from couchpotato.runner import runCouchPotato
if __name__ == '__main__': def main():
if os.environ.get('cp_main', 'false') == 'true': if os.environ.get('cp_main', 'false') == 'true':
try: try:
runCouchPotato(options, base_path, sys.argv[1:]) runCouchPotato(options, base_path, sys.argv[1:])
@@ -61,3 +57,16 @@ if __name__ == '__main__':
fireEvent('app.crappy_shutdown', single = True) fireEvent('app.crappy_shutdown', single = True)
time.sleep(0.1) time.sleep(0.1)
sys.exit() sys.exit()
from daemon import Daemon
class CouchDaemon(Daemon):
def run(self):
os.environ['cp_main'] = 'true'
main()
if __name__ == '__main__':
if options.daemon and options.pid_file:
daemon = CouchDaemon(options.pid_file)
daemon.start()
else:
main()
+4
View File
@@ -27,6 +27,10 @@ def getOptions(base_path, args):
dest = 'quiet', help = 'No console logging') dest = 'quiet', help = 'No console logging')
parser.add_argument('--nogit', action = 'store_true', parser.add_argument('--nogit', action = 'store_true',
dest = 'nogit', help = 'No git available') dest = 'nogit', help = 'No git available')
parser.add_argument('--daemon', action = 'store_true',
dest = 'daemon', help = 'Daemonize the app')
parser.add_argument('--pid_file', default = os.path.join(data_dir, 'couchpotato.pid'),
dest = 'pid_file', help = 'Path to pidfile needed for daemon')
options = parser.parse_args(args) options = parser.parse_args(args)
+117 -166
View File
@@ -1,181 +1,132 @@
## {{{ http://code.activestate.com/recipes/278731/ (r6) #!/usr/bin/env python
"""Disk And Execution MONitor (Daemon)
Configurable daemon behaviors: from signal import SIGTERM
import sys
import os
import time
import atexit
1.) The current working directory set to the "/" directory. class Daemon():
2.) The current file creation mode mask set to 0.
3.) Close all open files (1024).
4.) Redirect standard I/O streams to "/dev/null".
A failed call to fork() now raises an exception.
References:
1) Advanced Programming in the Unix Environment: W. Richard Stevens
2) Unix Programming Frequently Asked Questions:
http://www.erlenstar.demon.co.uk/unix/faq_toc.html
"""
__author__ = "Chad J. Schroeder"
__copyright__ = "Copyright (C) 2005 Chad J. Schroeder"
__revision__ = "$Id$"
__version__ = "0.2"
# Standard Python modules.
import os # Miscellaneous OS interfaces.
#import sys # System-specific parameters and functions.
# Default daemon parameters.
# File mode creation mask of the daemon.
UMASK = 0
# Default working directory for the daemon.
WORKDIR = "/"
# Default maximum for the number of available file descriptors.
MAXFD = 1024
# The standard I/O file descriptors are redirected to /dev/null by default.
if (hasattr(os, "devnull")):
REDIRECT_TO = os.devnull
else:
REDIRECT_TO = "/dev/null"
def createDaemon():
"""Detach a process from the controlling terminal and run it in the
background as a daemon.
""" """
A generic daemon class.
try: Usage: subclass the Daemon class and override the run() method
# Fork a child process so the parent can exit. This returns control to """
# the command-line or shell. It also guarantees that the child will not def __init__(self, pidfile, stdin = '/dev/null', stdout = '/dev/null', stderr = '/dev/null'):
# be a process group leader, since the child receives a new process ID self.stdin = stdin
# and inherits the parent's process group ID. This step is required self.stdout = stdout
# to insure that the next call to os.setsid is successful. self.stderr = stderr
pid = os.fork() self.pidfile = pidfile
except OSError, e:
raise Exception, "%s [%d]" % (e.strerror, e.errno)
if (pid == 0): # The first child.
# To become the session leader of this new session and the process group
# leader of the new process group, we call os.setsid(). The process is
# also guaranteed not to have a controlling terminal.
os.setsid()
# Is ignoring SIGHUP necessary?
#
# It's often suggested that the SIGHUP signal should be ignored before
# the second fork to avoid premature termination of the process. The
# reason is that when the first child terminates, all processes, e.g.
# the second child, in the orphaned group will be sent a SIGHUP.
#
# "However, as part of the session management system, there are exactly
# two cases where SIGHUP is sent on the death of a process:
#
# 1) When the process that dies is the session leader of a session that
# is attached to a terminal device, SIGHUP is sent to all processes
# in the foreground process group of that terminal device.
# 2) When the death of a process causes a process group to become
# orphaned, and one or more processes in the orphaned group are
# stopped, then SIGHUP and SIGCONT are sent to all members of the
# orphaned group." [2]
#
# The first case can be ignored since the child is guaranteed not to have
# a controlling terminal. The second case isn't so easy to dismiss.
# The process group is orphaned when the first child terminates and
# POSIX.1 requires that every STOPPED process in an orphaned process
# group be sent a SIGHUP signal followed by a SIGCONT signal. Since the
# second child is not STOPPED though, we can safely forego ignoring the
# SIGHUP signal. In any case, there are no ill-effects if it is ignored.
#
# import signal # Set handlers for asynchronous events.
# signal.signal(signal.SIGHUP, signal.SIG_IGN)
def daemonize(self):
"""
do the UNIX double-fork magic, see Stevens' "Advanced
Programming in the UNIX Environment" for details (ISBN 0201563177)
http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
"""
try: try:
# Fork a second child and exit immediately to prevent zombies. This pid = os.fork()
# causes the second child process to be orphaned, making the init if pid > 0:
# process responsible for its cleanup. And, since the first child is # exit first parent
# a session leader without a controlling terminal, it's possible for sys.exit(0)
# it to acquire one by opening a terminal in the future (System V-
# based systems). This second fork guarantees that the child is no
# longer a session leader, preventing the daemon from ever acquiring
# a controlling terminal.
pid = os.fork() # Fork a second child.
except OSError, e: except OSError, e:
raise Exception, "%s [%d]" % (e.strerror, e.errno) sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
if (pid == 0): # The second child. # decouple from parent environment
# Since the current working directory may be a mounted filesystem, we os.chdir("/")
# avoid the issue of not being able to unmount the filesystem at os.setsid()
# shutdown time by changing it to the root directory. os.umask(0)
os.chdir(WORKDIR)
# We probably don't want the file mode creation mask inherited from
# the parent, so we give the child complete control over permissions.
os.umask(UMASK)
else:
# exit() or _exit()? See below.
os._exit(0) # Exit parent (the first child) of the second child.
else:
# exit() or _exit()?
# _exit is like exit(), but it doesn't call any functions registered
# with atexit (and on_exit) or any registered signal handlers. It also
# closes any open file descriptors. Using exit() may cause all stdio
# streams to be flushed twice and any temporary files may be unexpectedly
# removed. It's therefore recommended that child branches of a fork()
# and the parent branch(es) of a daemon use _exit().
os._exit(0) # Exit parent of the first child.
# Close all open file descriptors. This prevents the child from keeping # do second fork
# open any file descriptors inherited from the parent. There is a variety
# of methods to accomplish this task. Three are listed below.
#
# Try the system configuration variable, SC_OPEN_MAX, to obtain the maximum
# number of open file descriptors to close. If it doesn't exists, use
# the default value (configurable).
#
# try:
# maxfd = os.sysconf("SC_OPEN_MAX")
# except (AttributeError, ValueError):
# maxfd = MAXFD
#
# OR
#
# if (os.sysconf_names.has_key("SC_OPEN_MAX")):
# maxfd = os.sysconf("SC_OPEN_MAX")
# else:
# maxfd = MAXFD
#
# OR
#
# Use the getrlimit method to retrieve the maximum file descriptor number
# that can be opened by this process. If there is not limit on the
# resource, use the default value.
#
import resource # Resource usage information.
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
if (maxfd == resource.RLIM_INFINITY):
maxfd = MAXFD
# Iterate through and close all file descriptors.
for fd in range(0, maxfd):
try: try:
os.close(fd) pid = os.fork()
except OSError: # ERROR, fd wasn't open to begin with (ignored) if pid > 0:
pass # exit from second parent
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# Redirect the standard I/O file descriptors to the specified file. Since # redirect standard file descriptors
# the daemon has no controlling terminal, most daemons redirect stdin, sys.stdout.flush()
# stdout, and stderr to /dev/null. This is done to prevent side-effects sys.stderr.flush()
# from reads and writes to the standard I/O file descriptors. si = file(self.stdin, 'r')
so = file(self.stdout, 'a+')
se = file(self.stderr, 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
# This call to open is guaranteed to return the lowest file descriptor, # write pidfile
# which will be 0 (stdin), since it was closed above. atexit.register(self.delpid)
os.open(REDIRECT_TO, os.O_RDWR) # standard input (0) pid = str(os.getpid())
file(self.pidfile, 'w+').write("%s\n" % pid)
# Duplicate standard input to standard output and standard error. def delpid(self):
os.dup2(0, 1) # standard output (1) os.remove(self.pidfile)
os.dup2(0, 2) # standard error (2)
return(0) def start(self):
"""
Start the daemon
"""
# Check for a pidfile to see if the daemon already runs
try:
pf = file(self.pidfile, 'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
if pid:
message = "pidfile %s already exist. Daemon already running?\n"
sys.stderr.write(message % self.pidfile)
sys.exit(1)
# Start the daemon
self.daemonize()
self.run()
def stop(self):
"""
Stop the daemon
"""
# Get the pid from the pidfile
try:
pf = file(self.pidfile, 'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
if not pid:
message = "pidfile %s does not exist. Daemon not running?\n"
sys.stderr.write(message % self.pidfile)
return # not an error in a restart
# Try killing the daemon process
try:
while 1:
os.kill(pid, SIGTERM)
time.sleep(0.1)
except OSError, err:
err = str(err)
if err.find("No such process") > 0:
if os.path.exists(self.pidfile):
os.remove(self.pidfile)
else:
print str(err)
sys.exit(1)
def restart(self):
"""
Restart the daemon
"""
self.stop()
self.start()
def run(self):
"""
You should override this method when you subclass Daemon. It will be called after the process has been
daemonized by start() or restart().
"""