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 another example that
does not use a scheduler, see the pyserv httpdaemon
script.
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 -U -f https://github.com/sarnold/python-daemonizer/releases/ daemonizer
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