# Python daemonize (3.x) ###### tags: `By_Ivan` Reference: [reference src](https://github.com/metachris/python-posix-daemon), [python daemon](https://pypi.org/project/python-daemon/) [TOC] ## What is Daemon in Unix [wiki](https://en.wikipedia.org/wiki/Daemon_%28computing%29) TL;DR A daemon is a background, non-interactive program. It is detached from the keyboard and display of any interactive user. The word "daemon" for denoting a background program is from the Unix culture; it is not universal. [>ref](https://stackoverflow.com/questions/28139377/daemon-and-service-difference) >**PEP 3143 -- Standard daemon process library** >[name=Ben Finney <ben+python at benfinney.id.au>][time=01 26, 2009] >[reference link](https://www.python.org/dev/peps/pep-3143/) >>**Correct daemon behaviour** >> >>According to Stevens in [stevens] ยง2.6, a program should perform the following steps to become a Unix daemon process. >> * Close all open file descriptors. >> * Change current working directory. >> * Reset the file access creation mask. >> * Run in the background. >> * Disassociate from process group. >> * Ignore terminal I/O signals. >> * Disassociate from control terminal. >> * Don't reacquire a control terminal. >> * Correctly handle the following circumstances: >> * Started by System V init process. >> * Daemon termination by SIGTERM signal. >> * Children generate SIGCLD signal. >>> **Unix Network Programming** >>> [name=W. Richard Stevens, Prentice Hall.][time=1994] ## Difference between Daemon and Service **Service:** * A service is a program which responds to requests. A service is what a server provides. * Requests are transfered over some inter-process communication mechanism (usually over a network). * For example, the NFS port mapping service is provided as a separate portmap service, which is implemented as the portmapd daemon. A service doesn't have to be a daemon, but usually is. A user application with a GUI could have a service built into it: for instance, a file-sharing application. ## Simple Daemon Implementation in python Not all of the steps are required to achieve daemon program. The solution has many approaches and many template exist on internet. In this project, I'm using a open source daemon framwork to daemonize my program. [reference src](https://github.com/metachris/python-posix-daemon) This code support ### Usage Import the daemon framework from daemonize. Then create a app class inheriting the class object. Class function run() must be defined and overide the original. pidfile is used for recording the daemon's PID. ```python= from daemonize import daemon class app_subclass(daemon): def __init__(self): self.pidfile = "pidfile" # path for pidfile # ... def run(self): while True: # do something # ... ``` In the main function: Read from the command line argument, then called the corresponding function. ```python= def main(arg): APP = app_subclass() if arg == "start": # start without daemonize APP.start() elif arg == "daemon": # run in daemon APP.start(if_daemon=True) elif arg == "stop": # send SIGTERM to the PID in pidfile APP.stop() elif arg == "restart": # stop, then start APP.restart() else: # argument error exit(1) ``` ### Workflow ![](https://i.imgur.com/sSYhNu4.png) ## Room for improvement 1. Security: user group require the program to run on distinct user, then lock the config file (or even output file). 1. Better solution on output stream 1. Class object for control Seperate the control function from the daemon class. This could make the function layer clearer and safer. 1. Logging to show daemon's status 1. Handle multiple running daemon Currently, only one daemon can exist at the same time. ___