I have Python script bgservice.py
and I want it to run all the time, because it is part of the web service I build. How can I make it run continuously even after I logout SSH?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can nohup it, but I prefer screen.
If what you need is that the process should run forever no matter whether you are logged in or not, consider running the process as a daemon.
supervisord is a great out of the box solution that can be used to daemonize any process. It has another controlling utility
supervisorctl
that can be used to monitor processes that are being run by supervisor.You don't have to write any extra code or modify existing scripts to make this work. Moreover, verbose documentation makes this process much simpler.
After scratching my head for hours around python-daemon, supervisor is the solution that worked for me in minutes.
Hope this helps someone trying to make python-daemon work
Run
nohup python bgservice.py &
to get the script to ignore the hangup signal and keep running. Output will be put innohup.out
.Ideally, you'd run your script with something like
supervise
so that it can be restarted if (when) it dies.You might consider turning your python script into a proper python daemon, as described here.
python-daemon is a good tool that can be used to run python scripts as a background daemon process rather than a forever running script. You will need to modify existing code a bit but its plain and simple.
If you are facing problems with python-daemon, there is another utility supervisor that will do the same for you, but in this case you wont have to write any code (or modify existing) as this is a out of the box solution for daemonizing processes.
If you've already started the process, and don't want to kill it and restart under nohup, you can send it to the background, then disown it.
Ctrl+Z
(suspend the process)bg
(restart the process in the backgrounddisown %1
(assuming this is job #1, usejobs
to determine)Here is a simple solution inside python using a decorator:
You can of course replace the content of your
bgservice.py
file in place ofmy_func
.