socktools.daemon.base_daemon - UNIX daemon basic implementation¶
This module provides the base implementation for a unix daemon.
Daemons in socktools are implemented as a collection of protocols with appropriate infrastructure for handling logging etc.
Parts are inspired by or copied from python-daemon as found at https://github.com/martinrusev/python-daemon. See docs/python-daemon-license.txt for the license for python-daemon.
Note
This will almost certainly NOT work on windows - but if you’re using windows to host a server you’re too incompetent to bother to read this documentation anyway.
-
class
socktools.daemon.base_daemon.BaseDaemon(pidfile='./daemon.pid', stdin='/dev/null', stdout='/dev/null', stderr='/dev/null')[source]¶ Bases:
objectThe base daemon class from which others inherit
This class can be used directly in theory, but it makes more sense to inherit from it or use one of the other daemon classes.
Keyword Arguments: - pidfile (str) – Path to the PID file the daemon will use
- stdin (str) – Path to the stdin device to use
- stdout (str) – Path to the stdout device
- stderr (str) – Path to the stderr device
-
atexit_handler()[source]¶ Handle cleanup before closing
This method is called if the daemon process closes and cleans up things like the PID file. If subclassing, you must make sure to call the parent method here.
Note
This method should ONLY do cleanup, after it returns the process dies
-
daemonize()[source]¶ Daemonize the process
This is where the magic happens, this method is called by start() to daemonize the process and start running. You should not call this directly, instead use start().
Warning
This method does not check if the daemon is already running or not, if it is already running then bad things will happen
-
do_reload()[source]¶ Reload configuration
This method should be overridden when inheriting from the class. Your implementation should reload any configuration from disk. In the default implementation this does nothing.
-
fork_me()[source]¶ Used internally - fork me baby, fork me
Used internally to fork when detaching from parent process, fork me baby, fork me
-
get_logger()[source]¶ Sets up the default logger
This method is used at startup to create a simple logger. The default implementation writes to syslog.
Returns: the logger Return type: logging.Logger
-
get_pid()[source]¶ Get the pid of the active daemon process
If the daemon is running, this will give you the PID, otherwise it’ll return None
Returns: the PID of the active daemon process or None Return type: int
-
handle_hup(signum, frame)[source]¶ Signal handler for SIGHUP
This method is the default signal handler for SIGHUP. The standard action to take upon a SIGHUP is reloading configuration, so the default implementation calls self.do_reload().
Parameters: - signum (int) – Always signal.SIGHUP
- frame – A stack frame, this will be returned to once the handler has finished running
-
handle_rc(argv=['/home/docs/checkouts/readthedocs.org/user_builds/python-sock-tools/envs/latest/bin/sphinx-build', '-T', '-E', '-b', 'readthedocs', '-d', '_build/doctrees-readthedocs', '-D', 'language=en', '.', '_build/html'])[source]¶ Implements rc.d style style commands
To use a daemon in production you probably want a script to put into the init system, that’s what this method is for. Simply invoke it and it’ll handle start/stop/reload/restart/status.
If the arguments provided are not valid, this method will dump usage information to stdout and then exit.
Keyword Arguments: argv (list) – List of command line arguments, generally this should just be sys.argv
-
is_running()[source]¶ Check if the daemon is already running, either in this or in another process
This method will check first if the current process is running the daemon and if not will check the pidfile and check if the PID is still an active process.
Returns: True if the daemon is already running, otherwise False Return type: bool
-
pre_stdout(pid=0)[source]¶ Called just before we redirect the file descriptors
This method is called just before we redirect the file descriptors and make normal output no longer work - use it for announcing startup messages etc. In the default implementation this outputs a message announcing the PID of the daemon.
Warning
DO NOT use print to output here, use sys.stderr.write() and then sys.stderr.flush() otherwise bad things will happen.
Keyword Arguments: pid (int) – The PID of the daemon process
-
run()[source]¶ Main loop of the daemon process
This method is where the actual server (or whatever) code goes. The default implementation just runs eventlet.greenthread.sleep(3600) in an infinite loop. The default implementation also spits out a log entry once per hour.