This is a Python class that will daemonize your Python script so it can continue running in the background. It works on Unix, Linux and OS X, creates a PID file and has standard commands (start, stop, restart) plus a foreground mode.
Based on this original version from jejik.com.
Define a class which inherits from Daemon
and has a run()
method
(which is what will be called once the daemonization is completed).
from daemon import Daemon
class pantalaimon(Daemon):
def run(self):
do_mything()
Depending on what your code is doing, you may also want to subclass the
cleanup()
method to close sockets, etc (called after stop()
and
before termination).
Create a new object of your class, specifying where you want your PID file to exist:
pineMarten = pantalaimon("/path/to/pid.pid")
pineMarten.start()
start
- start the daemon (create PID and daemonize)stop
- stop the daemon (stop the child process and remove the PID)restart
- runstop
thenstart
status
- show daemon status
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 instead of start()
:
pineMarten.run()
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 schedule
module to execute code snippets repeatedly (examples). For daemon
examples see the next section.
Pyserv is a growing collection of threaded Python server bits, including custom HTTP server and WSGI classes, along with corresponding console entry points and some daemon scripts. The latest addition includes an async version of the original tftpy-based daemon script.
The httpdaemon
, atftpdaemon
, and tftpdaemon
commands are
stand-alone Python daemon scripts with the same core server code, as
well as a default user configuration adjustable via environment
variables.
This updated version is not published on PyPI, thus use one of the following commands to install the latest python-daemonizer in a Python virtual environment on any platform.
From source:
$ python3 -m venv env $ source env/bin/activate $ pip install git+https://github.com/sarnold/python-daemonizer.git
The alternative to python venv is the Tox test driver. If you have it installed already, clone this repository and try the following commands from the python-daemonizer source directory.
To install in dev mode and run the tests:
$ tox -e py
To run pylint:
$ tox -e lint
Note
After installing in dev mode, use the environment created by
Tox just like any other Python virtual environment. The dev
install mode of Pip allows you to edit the script and run it
again while inside the virtual environment. By default Tox
environments are created under .tox/
and named after the
env argument (eg, py).
To install the latest release, eg with your own tox.ini
file in
another project, use something like this:
$ pip install https://github.com/sarnold/python-daemonizer/releases/download/0.4.0/daemonizer-0.4.0-py3-none-any.whl
That said, the "proper" form for git-based dependencies would be more like this:
daemonizer @ git+https://github.com/sarnold/[email protected]#69d4c9b6f3916cd5ae87c8b3a78eca1015eeed0f
This repo is now pre-commit enabled for python/rst source and file-type linting. The checks run automatically on commit and will fail the commit (if not clean) and perform simple file corrections. For example, if the mypy check fails on commit, you must first fix any fatal errors for the commit to succeed. That said, pre-commit does nothing if you don't install it first (both the program itself and the hooks in your local repository copy).
You will need to install pre-commit before contributing any changes; installing it using your system's package manager is recommended, otherwise install with pip into your usual virtual environment using something like:
$ sudo emerge pre-commit --or-- $ pip install pre-commit
then install it into the repo you just cloned:
$ git clone https://github.com/sarnold/python-daemonizer $ cd python-daemonizer/ $ pre-commit install
It's usually a good idea to update the hooks to the latest version:
$ pre-commit autoupdate
Most (but not all) of the pre-commit checks will make corrections for you, however, some will only report errors, so these you will need to correct manually.
Automatic-fix checks include ffffff, isort, autoflake, and miscellaneous
file fixers. If any of these fail, you can review the changes with
git diff
and just add them to your commit and continue.
If any of the mypy, bandit, or rst source checks fail, you will get a report, and you must fix any errors before you can continue adding/committing.
To see a "replay" of any rst
check errors, run:
$ pre-commit run rst-backticks -a $ pre-commit run rst-directive-colons -a $ pre-commit run rst-inline-touching-normal -a
To run all pre-commit
checks manually, try:
$ pre-commit run -a
This project is now compliant with the REUSE Specification Version 3.3, so the
corresponding license information for all files can be found in the REUSE.toml
configuration file with license text(s) in the LICENSES/
folder.
Related metadata can be (re)generated with the following tools and command examples.
- reuse-tool - REUSE compliance linting and sdist (source files) SBOM generation
- sbom4python - generate SBOM with full dependency chain
Use tox to create the environment and run the lint command:
$ tox -e reuse # --or-- $ tox -e reuse -- spdx > sbom.txt # generate sdist files sbom
Note you can pass any of the other reuse commands after the --
above.
Use the above environment to generate the full SBOM in text format:
$ source .tox/reuse/bin/activate $ sbom4python --system --use-pip -o <file_name>.txt
Be patient; the last command above may take several minutes. See the doc links above for more detailed information on the tools and specifications.