1. Description of the problem
Requires a Python service program to run in the background, not to let the process be killed, even if killed to be able to automatically restart in a timely manner. For example, there is a Python program: test.py, which runs the program by command: Python test.py, but it is interrupted by a command line interrupt. So we need a way to keep the program running in the background.
2. Workaround
I used to always use the command: Nohup python test.py & to keep it running uninterrupted in the background, but that's not guaranteed to run all the time.
The following describes the process of using supervisor to manage Python, keeping it running uninterrupted in the background.
(1) Installation Supervisor
On the command line input: sudo apt-get install supervisor for installation supervisor
To test whether the installation was successful, enter at the command line: Supervis and then press the TAB key to see if it can complete the command: supervisor, if the installation is successful
(2) Configuring the supervisord.conf file
Locate the supervisord.conf configuration file, usually under the/etc/directory, or under the/etc/supervisor/directory, open the file with root privileges: sudo vim supervisord.conf, otherwise it cannot be modified.
Add the following lines at the end of the file:
[Program:myprogram]
command=python/home/myname/test.py
Autostart=true
Autorestart=ture
Stdout_logfile=/home/myname/test.log
Note: the first line of MyProgram a name to indicate your project.
The second line of python/home/myname/test.py shows you the command to run the program
The third line indicates auto-start, and if the value is false, it does not start automatically
Row Four indicates an automatic restart, and a value of false indicates that it does not restart automatically
Finally, the program prints out the information that is recorded in the Test.log file, which is the log file
(3) using Supervisor
After the configuration is complete, the command line input: sudo supervisord is ok.
You can kill the process of your program and then look and see it automatically restarts without worrying about being killed.
Well, I hope it will be helpful to everyone.
--------------------------------------------------------------------------------------------------------------- --------------------------
"Python" uses supervisor to manage the Python process