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
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.