Python daemon (background service)
Using the python-damon module:
examples/python/mydaemon.py
import datetime import time import daemon def main_program(): while True: with open('/tmp/echo.txt', 'a') as fh: fh.write("{}\n".format(datetime.datetime.now())) time.sleep(1) with daemon.DaemonContext(): main_program()
Install the module
pip install python-daemon
Launch the damon in the background
python mydaemon.py
Check that it is working
tail -f /tmp/echo.txt
Find the process id
$ ps axuw | grep mydaemon gabor 7686 1.7 0.0 32692 15860 ? S 06:50 0:00 python examples/python/mydaemon.py gabor 7692 0.0 0.0 14352 920 pts/1 S+ 06:50 0:00 grep --color=auto mydaemon
Stop the process
Use the process ID to stop the service:
kill 7686
Published on 2019-06-25
If you have any comments or questions, feel free to post them on the source of this page in GitHub. Source on GitHub.
Comment on this post