How do you create a daemon in Python?

后端 未结 16 2033
轻奢々
轻奢々 2020-11-22 01:55

Searching on Google reveals x2 code snippets. The first result is to this code recipe which has a lot of documentation and explanation, along with some useful discussion und

16条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-22 02:49

    Probably not a direct answer to the question, but systemd can be used to run your application as a daemon. Here is an example:

    [Unit]
    Description=Python daemon
    After=syslog.target
    After=network.target
    
    [Service]
    Type=simple
    User=
    Group=
    ExecStart=/usr/bin/python /script.py
    
    # Give the script some time to startup
    TimeoutSec=300
    
    [Install]
    WantedBy=multi-user.target
    

    I prefer this method because a lot of the work is done for you, and then your daemon script behaves similarly to the rest of your system.

    -Orby

提交回复
热议问题