Home >Backend Development >Python Tutorial >Take you to understand the Python process management tool Supervisor
This article brings you relevant knowledge about Python. Supervisor is a process management system developed in Python, which allows users to monitor and control processes on Linux. It can convert an ordinary command line The process becomes a background daemon process and can be automatically restarted when it exits abnormally. Let's take a look at it. I hope it will be helpful to everyone.
【Related recommendations: Python3 video tutorial】
Supervisor is a process management system developed in Python that allows users to monitor and control processes on Linux. It can turn an ordinary command line process into a background daemon process and automatically restart when it exits abnormally
Supervisor supports Linux and Mac, but does not support Windows
The system for this article is: centos, supervisor==4.2.4
pip3 install supervisor
After the installation is complete, there will be several files like this under the Python bin directory
.
ls /usr/local/Python38/bin echo_supervisord_conf supervisorctl supervisord
Note: Since the python bin directory has added environment variables, these files can be executed directly.
1. First create a directory to store the configuration file: mkdir supervisord
.
echo_supervisord_conf > supervisord/supervisord.conf
If an error is reported -bash: /etc/supervisord.conf: Permission denied, you need to switch to the root user.
2. Create the child process configuration file path
mkdir -p supervisor/conf.d
Our subsequent tasks, we want to use it as a daemon process, all need a configuration file, we put these configuration files in conf.d below the directory.
3. Modify the configuration file
vim supervisord/supervisord.conf
Change the last part to
[inclue] # 因为我这里是放在root用户目录下,也可以放其它目录 files=/root/supervisord/conf.d/*.conf
1. Start supervisord
supervisord -c supervisord/supervisord.conf
This will start supervisord
. We can then hand over our tasks to him to guard. If it stops, it will automatically restart for us.
View version
supervisord -v
2. Write a simple Shell script
vim supervisord/test.sh
The content is as follows
#!/bin/bash while : do echo `date '+%Y-%m-%d %H:%m:%S'` sleep 1 done
Simple run, Ctrl C
Exit
3. Create a child process configuration file
vim supervisor/conf.d/test.conf
test.conf
The content is as follows:
[program:test] command=sh /root/supervisord/test.sh priority=999 ; 相对启动优先级,数值越小越优先,默认为999 autostart=true ; 在supervisor启动时自动启动,默认为true autorestart=true ; 在意外退出时重新启动,默认为true startsecs=10 ; 子进程启动多少秒后状态为running则认为启动成功,默认为1 startretries=3 ; 尝试启动的最大次数,默认为3 exitcodes=0,2 ; 进程的预期退出代码列表,默认为0 stopsignal=QUIT ; 终止进程的信号,默认为TERM stopwaitsecs=10 ; 在SIGKILL之前等待的最大秒数,默认为10 user=root ; 在某用户下设置uid来启动程序,默认不切换用户 redirect_stderr=true ; 是否重定向stdout和stderr,默认为false stdout_logfile=/tmp/supervisor.stdout.log ; stdout的输出文件,默认为AUTO stdout_logfile_maxbytes=50MB ; stdout最大文件大小,默认为50MB stdout_logfile_backups=10 ; stdout文件备份数,设为0则不备份,默认为10
In fact, you only need to configure 3 parameters, and you don’t need to worry about the others:
command=sh /root/supervisord/test.sh
: Our sub-process startup command; stdout_logfile=/tmp/supervisor. stdout.log
: Log; program:test
: The process name is test
. If the process wants to stop and start on any day, the process name is required; The current file directory structure is like this:
yum install tree tree supervisord supervisord ├── conf.d │ └── test.conf ├── supervisord.conf └── test.sh
4. Reread the configuration and update the child process
Because our supervisord
has Started, you can view it through ps -ef | grep supervisord.conf
. The sub-process configuration file has been added and needs to be reloaded:
First enter the supervisord
directory: cd supervisord
, otherwise there will be problems when executing the following command.
supervisorctl reread
Check the process status again
supervisorctl status
Result:
test RUNNING pid 30278, uptime 1:29:41
name The process for test
is already running in the background as a daemon process. Let's kill it:
kill 30278
Execute supervisorctl status
again, and you will find that the status immediately changes from starting
will soon become running
, then the role of supervisord
is already obvious, it can automatically help us monitor tasks automatically.
Note: For commands related to adding, deleting, starting, and stopping child processes, see the appendix.
The web interface is not very useful, that is, if you want to start or pause a process, you don't need to type commands.
vim supervisord.conf
Uncomment
[inet_http_server] port=*:9001 ; 此处改为*便于调试
Restart supervisord
supervisorctl reload
Browser access: linux_ip:9001.
Add a new configuration file, reload
supervisorctl reread
Change a configuration file, reload
supervisorctl update
Restart supervisord
supervisorctl reload
View all process status
supervisorctl status
View Specify process status
supervisorctl status <name>
Start all child processes
supervisorctl start all
Start specified child processes
supervisorctl start <name>
Restart all child processes
supervisorctl restart all
Restart specified child processes
supervisorctl restart <name>
Stop all child processes
supervisorctl stop all
Stop the specified child process
supervisorctl stop <name>
Add a child process to the process group
supervisorctl add <name>
To remove a child process from the process group, you need to stop first . Note: After removal, you need to use reread and update to re-run the process
supervisorctl reomve <name>
[Related recommendations: Python3 video tutorial]
The above is the detailed content of Take you to understand the Python process management tool Supervisor. For more information, please follow other related articles on the PHP Chinese website!