Skip to main content
License PyPi Version PY Versions Build Status Test Coverage

License: Creative Commons

Overview

This is a Python class that will daemonize your Python code so it can continue running in the background. It works on Unix and Linux systems. It creates a PID file that is controlled by system file locks so the need to remove a stale pid file is not necessary.

The Daemon class will write to a log file if you set one up. Any code that uses the Daemon class can also write to the same log file making it easy to have a combined log.

The Daemon class has standard start, stop, restart commands plus a foreground mode. This current version no longer supports Python 2. This code may still support OSX but since I don’t have a machine that runs OSX I cannot test it.

This code is based on the original version from jejik.com.

Usage

Define a class which inherits from Daemon and has a run() method which is what will be called once the daemonization is completed.

from daemonize import Daemon

class MyDaemon(Daemon):

    def run(self, *args, **kwards):
        # Do stuff

    def stop_callback(self):
        # Do anything you need to do before a signal shuts
        # down your daemon.

A while loop set to True will usually work fine in the run() method or you could use something more sophisticated as mentioned below in the last paragraph under Continuous Execution.

There is also a stop_callback method that can be overridden in your sub-class to the Daemon class. It gives your daemon the ability to perform certain tasks before a shutdown is performed, like closing files etc.

Create a new object of your class, specifying where you want your PID file to exist:

md = MyDaemon('/path/to/pid.pid')
md.start()

There are a lot of arguments that can be used in the constructor of the Daemon class.

Positional Arguments

  • pidfile: Mandatory fullpath including file name of the pid file.

Keyword Arguments

  • stdin: The input data stream, defaults to os.devnull.

  • stdout: The output data stream, defaults to os.devnull.

  • stderr: The error data stream, defaults to os.devnull.

  • base_dir: The absolute or relative path to the directory that will be used after the process is detached from the terminal, defaults to ‘.’ (current working directory).

  • umask: The mask used for any files created by the daemon, defaults to 0o22.

  • verbose: The default logging level. The values 1 = INFO, 2 = DEBUG, 3 = ERROR, and any number (other than 1, 2, 3) = WARNING, defaults to 1.

  • use_gevent: Use gevent to handle the killing of the daemon, defaults to False.

  • logger_name: The logger name used for logging, defaults to ‘’ (the root logger).

Run Actions

  1. start() – Starts the daemon (creates PID and daemonizes).

  2. stop() – Stops the daemon (stops the child process and removes the PID).

  3. restart() – Does a stop() and then start().

To start, restart, and stop the test daemon that is at the end of the daemonize/daemon.py code see below. Just substitute your code for the code below.

$ ps -ajx | grep daemonize/daemon | grep -v grep # No daemon running
$ python daemonize/daemon.py start
$ ps -ajx | grep daemonize/daemon | grep -v grep
PPID     PID    PGID     SID TTY        TPGID STAT   UID   TIME COMMAND
   1  425677  425676  425676 ?             -1 S     1000   0:00 python daemonize/daemon.py start
$ python daemonize/daemon.py restart
$ ps -ajx | grep daemonize/daemon | grep -v grep
PPID     PID    PGID     SID TTY        TPGID STAT   UID   TIME COMMAND
   1  425823  425822  425822 ?             -1 S     1000   0:00 python daemonize/daemon.py restart
$ python daemonize/daemon.py stop
$ ps -ajx | grep daemonize/daemon | grep -v grep # No daemon running

Foreground

This is useful for debugging because you can start the code without making it a daemon. The running script then depends on the open shell like any normal Python script.

To do this, just call the run() method directly.

md = MyDaemon('/path/to/pid.pid')
md.run()

Continuous Execution

The run() method will be executed just once so if you want the daemon to be doing stuff continuously you may wish to use the [1] sched module to execute code repeatedly [2] example or just use a while True.

Footnotes

Metadata

Release files for python-daemonizer 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-daemonizer 1.0.1
File Size Uploaded
python_daemonizer-1.0.1.tar.gz 23.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-daemonizer 1.0.1
File Interpreter ABI Platform
python_daemonizer-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 46.1 kB

Release files / python_daemonizer-1.0.1.tar.gz

Download URL python_daemonizer-1.0.1.tar.gz
Size 23.3 kB
Tags Source
SHA-256 checksum
How to use checksums
cc18c0e8ade0c074531b862529616aa01f2bf64b245a030f47bb88e2420f2de8
BLAKE2b-256 checksum
How to use checksums
ba1f7bd810cbfdb036e9eb8cea520ea88c98360885d8faeca2e82ceec7a66a9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.16.5 cpython/3.13.13 HTTPX/0.28.1

Release files / python_daemonizer-1.0.1-py3-none-any.whl

Download URL python_daemonizer-1.0.1-py3-none-any.whl
Size 22.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf5d620aa3311dd75a7afb500369740b6c385afbf8862b9935bdd955e800096c
BLAKE2b-256 checksum
How to use checksums
304caab62d45b2cbf4f2dc01235f8acb4652b5de3e376ce10c0ad8b57f3039d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.16.5 cpython/3.13.13 HTTPX/0.28.1

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page