pyzenkit.daemonizer module¶
This module contains simple daemonization library, that takes care of all tasks necessary to correctly daemonize a process. Correct daemonization consists of following steps:
# Setup directories and limits
# Setup user and group permissions
# Terminal detachment with double fork and split session
# Setup signal handlers
# Close all open file descriptors (except for possible log files)
# Redirect stdin
, stdout
, stderr
to /dev/null
# Detect current PID and store it to appropriate PID file
# Remove PID file at process exit
These steps are performed during full daemonization. For many purposes it is however
usefull to be capable of some kind of lite daemonization, in which case almost
every step in previous list is done except for terminal detachment, closing all files
and redirecting std*
to /dev/null
. This can be particularly usefull during
testing or debugging, or even during production deployments for some kind of
stay in foreground feature, which still enables the users to control the running
application from outside with signals.
Usage example¶
Example usage is implemented directly within this module, please refer to source code. To view the result of demonstration please execute the module directly with Python3 interpretter.
-
pyzenkit.daemonizer.
daemonize
(chroot_dir=None, work_dir=None, umask=None, uid=None, gid=None, pid_file=None, files_preserve=None, signals=None)[source]¶ Perform full daemonization of currently running process. All of the function arguments are optinal, so that it is possible to easily turn on/off almost any part of daemonization process. For example omitting the
uid
andgid
arguments will result in process permissions not to be changed.NOTE: It would be possible to call daemonize_lite() method from within this method, howewer for readability purposes and to maintain correct ordering of the daemonization steps I decided against coding best practices and kept two separate methods with similar contents. It will be necessary to update both when making any improvements, however I do not expect them to change much and often, if ever.
- Parameters
chroot_dir (str) – Name of the chroot directory (may be
None
).work_dir (str) – Name of the work directory (may be
None
).umask (int) – Umask as octal number (eg.
0o002
or0o022
, may beNone
).uid (int) – User ID to which to drop the permissions (may be
None
).gid (int) – Group ID to which to drop the permissions (may be
None
).pid_file (str) – Full path to the PID file (may be
None
).files_preserve (list) – List of file handles to preserve from closing (may be
None
).signals (dict) – Desired signals to be handled as keys and their appropriate handlers as values (may be
None
).
-
pyzenkit.daemonizer.
daemonize_lite
(chroot_dir=None, work_dir=None, umask=None, uid=None, gid=None, pid_file=None, signals=None)[source]¶ Perform lite daemonization of currently running process. All of the function arguments are optinal, so that it is possible to easily turn on/off almost any part of daemonization process. For example omitting the
uid
andgid
arguments will result in process permissions not to be changed.The lite daemonization does everything full daemonization does but detaching from current session. This can be usefull when debugging daemons, because they can be tested, benchmarked and profiled more easily.
- Parameters
chroot_dir (str) – Name of the chroot directory (may be
None
).work_dir (str) – Name of the work directory (may be
None
).umask (int) – Umask as octal number (eg.
0o002
or0o022
, may beNone
).uid (int) – User ID to which to drop the permissions (may be
None
).gid (int) – Group ID to which to drop the permissions (may be
None
).pid_file (str) – Full path to the PID file (may be
None
).signals (dict) – Desired signals to be handled as keys and their appropriate handlers as values (may be
None
).
-
pyzenkit.daemonizer.
get_logger_files
(logger)[source]¶ Return file handlers of all currently active loggers.
Result from this method will be used during daemonization process to close all open file descriptors but those belonging to given logger service.
Warning
This method is hacking internal structure of external module and might stop working, although the related interface has been stable for a long time.
- Parameters
logger (logging.Logger) – Logger to be analyzed for open file descriptors.
- Returns
List of open file descriptors used by logging service.
- Return type
list