How do you create a daemon in Python?

后端 未结 16 1681
轻奢々
轻奢々 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:34

    After a few years and many attempts (I tried all the answers given here, but all of them had minor drawbacks at the end), now I realize that there is a better way than wanting to start, stop, restart a daemon directly from Python: use the OS tools instead.

    For example, for Linux, instead of doing python myapp start and python myapp stop, I do this to start the app:

    screen -S myapp python myapp.py    
    CTRL+A, D to detach
    

    or screen -dmS myapp python myapp.py to start and detach it in one command.

    Then:

    screen -r myapp
    

    to attach to this terminal again. Once in the terminal, it's possible to use CTRL+C to stop it.

提交回复
热议问题