Package Documentation


class pep3143daemon.DaemonContext(chroot_directory=None, working_directory='/', umask=0, uid=None, gid=None, prevent_core=True, detach_process=None, files_preserve=None, pidfile=None, stdin=None, stdout=None, stderr=None, signal_map=None)

Implementation of PEP 3143 DaemonContext class

This class should be instantiated only once in every program that has to become a Unix Daemon. Typically you should call its open method after you have done everything that may require root privileges. For example opening port <= 1024.

Each option can be passed as a keyword argument to the constructor, but can also be changed by assigning a new value to the corresponding attribute on the instance.

Altering attributes after open() is called, will have no effect. In future versions, trying to do so, will may raise a DaemonError.

  • chroot_directory (str) – Full path to the directory that should be set as effective root directory. If None, the root directory is not changed.
  • working_directory (str.) – Full Path to the working directory to which to change to. If chroot_directory is not None, and working_directory is not starting with chroot_directory, working directory is prefixed with chroot_directory.
  • umask (int.) – File access creation mask for this daemon after start
  • uid (int.) – Effective user id after daemon start.
  • gid (int.) – Effective group id after daemon start.
  • prevent_core (bool.) – Prevent core file generation.
  • detach_process (bool.) – If True, do the double fork magic. If the process was started by inet or an init like program, you may don´t need to detach. If not set, we try to figure out if forking is needed.
  • files_preserve (list) – List of integers, or objects with a fileno method, that represent files that should not be closed while daemoninzing.
  • pidfile (Instance of Class that implements a pidfile behaviour) – Instance that implements a pidfile, while daemonizing its acquire method will be called.
  • stdin (file object.) – Redirect stdin to this file, if None, redirect to /dev/null.
  • stdout (file object.) – Redirect stdout to this file, if None, redirect to /dev/null.
  • stderr (file object.) – Redirect stderr to this file, if None, redirect to /dev/null.
  • signal_map (instance of dict) – Mapping from operating system signal to callback actions.

Dummy function


True when this instances open method was called


Daemonize this process

Do everything that is needed to become a Unix daemon.

terminate(signal_number, stack_frame)

Terminate this process

Simply terminate this process by raising SystemExit. This method is called if signal.SIGTERM was received.

Check carefully if this really is what you want!

Most likely it is not!

You should implement a function/method that is able to cleanly shutdown you daemon. Like gracefully terminating child processes, threads. or closing files.

You can create a custom handler by overriding this method, ot setting a custom handler via the signal_map. It is also possible to set the signal handlers directly via signal.signal().


The working_directory property



class pep3143daemon.DaemonError

Exception raised by DaemonContext


class pep3143daemon.PidFile(pidfile)

PidFile implementation for PEP 3143 Daemon.

This Class can also be used with pythons ‘with’ statement.

Parameters:pidfile (str) – filename to be used as pidfile, including path

Acquire the pidfile.

Create the pidfile, lock it, write the pid into it and register the release with atexit.


Release the pidfile.

Close and delete the Pidfile.