Skip to content

unix

UNIX daemon implementation

Classes:

UNIXDaemon

UNIXDaemon(name: str = '', pidfile: Pidfile | None = None, working_directory: str = '/', umask: int = 0, dlogger: Logger | None = None, stop_daemon_signal: int = SIGTERM, *args, **kwargs)

Bases: Daemon

UNIX daemon
Base class to define a valid daemon logic to be run on UNIX distributions
To define a UNIX daemon, you must override UNIXDaemon.run(...) abstract method

Daemon constructor function

Parameters:

  • name

    (str, default: '' ) –

    Daemon name

  • pidfile

    (Pidfile | None, default: None ) –

    PID file

  • working_directory

    (str, default: '/' ) –

    Working directory for daemon

  • umask

    (int, default: 0 ) –

    umask

  • dlogger

    (Logger | None, default: None ) –

    Daemon logger

  • args

    (Tuple[Any, ...], default: () ) –

    Positional arguments

  • kwargs

    (Dict[str, Any], default: {} ) –

    Keyword arguments

Methods:

  • args –

    Function to access positional arguments (defined at daemon definition)

  • get_arguments –

    Function to get arguments

  • kwargs –

    Function to access keyword arguments (defined at daemon definition)

  • restart –

    Function to restart the daemon (it stops then starts the process)

  • run –

    Method to run the daemon.

  • set_lock –

    Function to set lock for PID file writes

  • start –

    Function to start the daemon

  • status –

    Function to get status of the current daemon

  • stop –

    Function to stop the daemon.

args

args() -> Tuple[Any, ...]

Function to access positional arguments (defined at daemon definition)
from daemon's body

Returns:

  • Tuple[Any, ...] –

    Positional arguments

get_arguments

get_arguments() -> Dict[str, Tuple[Any, ...] | Dict[str, Any]]

Function to get arguments
This method can be called in the custom daemon logic

Returns:

  • Dict[str, Tuple[Any, ...] | Dict[str, Any]] –

    Dict of both positional and keyword arguments

kwargs

kwargs() -> Dict[str, Any]

Function to access keyword arguments (defined at daemon definition)
from daemon's body

Returns:

  • Dict[str, Any] –

    Keyword arguments

restart

restart() -> None

Function to restart the daemon (it stops then starts the process)
Once restarted, daemon has a new PID (new process)

Returns:

  • None –

    Nothing

run

run() -> None

Method to run the daemon.
This method must be overridden by the child class.

Returns:

  • None –

    Nothing

set_lock

set_lock(lock: Lock | None) -> None

Function to set lock for PID file writes

Parameters:

  • lock

    (Lock | None) –

    Multiprocessing lock to protect PID file on-disk writes

Returns:

  • None –

    Nothing

start

start() -> None

Function to start the daemon

Returns:

  • None –

    Nothing

status

status()

Function to get status of the current daemon

Returns:

  • None –

    Nothing

stop

stop() -> None

Function to stop the daemon.
- If the PID file is on the disk (same name as current daemon), we read the PID in the file
- If process (identified by PID) is still active, we kill it with specified signal (self.stop_daemon_signal)
- If process is not active anymore (it may have been terminated by natural cause (core logic) or external
signal sent by the user), we are removing PID file (not needed anymore)
- If the PID file is not on the disk, we cannot stop the daemon.
This is why it is important to avoid manipulating PID files as their management is handled automatically by the
library.

Returns:

  • None –

    Nothing