1daemonize 2======================== 3 4 5.. image:: https://readthedocs.org/projects/daemonize/badge/?version=latest 6 :target: http://daemonize.readthedocs.org/en/latest/?badge=latest 7 :alt: Latest version 8 9.. image:: https://img.shields.io/travis/thesharp/daemonize.svg 10 :target: http://travis-ci.org/thesharp/daemonize 11 :alt: Travis CI 12 13.. image:: https://img.shields.io/pypi/dm/daemonize.svg 14 :target: https://pypi.python.org/pypi/daemonize 15 :alt: PyPI montly downloads 16 17.. image:: https://img.shields.io/pypi/v/daemonize.svg 18 :target: https://pypi.python.org/pypi/daemonize 19 :alt: PyPI last version available 20 21.. image:: https://img.shields.io/pypi/l/daemonize.svg 22 :target: https://pypi.python.org/pypi/daemonize 23 :alt: PyPI license 24 25 26**daemonize** is a library for writing system daemons in Python. It is 27distributed under MIT license. Latest version can be downloaded from 28`PyPI <https://pypi.python.org/pypi/daemonize>`__. Full documentation can 29be found at 30`ReadTheDocs <http://daemonize.readthedocs.org/en/latest/?badge=latest>`__. 31 32Dependencies 33------------ 34 35It is tested under following Python versions: 36 37- 2.6 38- 2.7 39- 3.3 40- 3.4 41- 3.5 42 43Installation 44------------ 45 46You can install it from Python Package Index (PyPI): 47 48:: 49 50 $ pip install daemonize 51 52Usage 53----- 54 55.. code-block:: python 56 57 from time import sleep 58 from daemonize import Daemonize 59 60 pid = "/tmp/test.pid" 61 62 63 def main(): 64 while True: 65 sleep(5) 66 67 daemon = Daemonize(app="test_app", pid=pid, action=main) 68 daemon.start() 69 70File descriptors 71---------------- 72 73Daemonize object's constructor understands the optional argument 74**keep\_fds** which contains a list of FDs which should not be closed. 75For example: 76 77.. code-block:: python 78 79 import logging 80 from daemonize import Daemonize 81 82 pid = "/tmp/test.pid" 83 logger = logging.getLogger(__name__) 84 logger.setLevel(logging.DEBUG) 85 logger.propagate = False 86 fh = logging.FileHandler("/tmp/test.log", "w") 87 fh.setLevel(logging.DEBUG) 88 logger.addHandler(fh) 89 keep_fds = [fh.stream.fileno()] 90 91 92 def main(): 93 logger.debug("Test") 94 95 daemon = Daemonize(app="test_app", pid=pid, action=main, keep_fds=keep_fds) 96 daemon.start() 97