Home > Backend Development > Python Tutorial > Take you to understand the Python process management tool Supervisor

Take you to understand the Python process management tool Supervisor

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2022-07-28 15:34:46
forward
2662 people have browsed it

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.

Take you to understand the Python process management tool Supervisor

【Related recommendations: Python3 video tutorial

1. Introduction

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

2. Installation

Supervisor supports Linux and Mac, but does not support Windows
The system for this article is: centos, supervisor==4.2.4

pip3 install supervisor
Copy after login

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
Copy after login

Note: Since the python bin directory has added environment variables, these files can be executed directly.

3. Create the configuration file

1. First create a directory to store the configuration file: mkdir supervisord.

echo_supervisord_conf > supervisord/supervisord.conf
Copy after login

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
Copy after login

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
Copy after login

Change the last part to

[inclue]
# 因为我这里是放在root用户目录下,也可以放其它目录
files=/root/supervisord/conf.d/*.conf
Copy after login

4. Preliminary test

1. Start supervisord

supervisord -c supervisord/supervisord.conf
Copy after login

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
Copy after login

2. Write a simple Shell script

vim supervisord/test.sh
Copy after login

The content is as follows

#!/bin/bash
while : 
do
    echo `date '+%Y-%m-%d %H:%m:%S'`
    sleep 1
done
Copy after login

Simple run, Ctrl C Exit

3. Create a child process configuration file

vim supervisor/conf.d/test.conf
Copy after login

test.confThe 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
Copy after login

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
Copy after login

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
Copy after login
Copy after login

Check the process status again

supervisorctl status
Copy after login
Copy after login

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
Copy after login

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.

5. Web interface

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
Copy after login

Uncomment

[inet_http_server]
port=*:9001                ; 此处改为*便于调试
Copy after login

Restart supervisord

supervisorctl reload
Copy after login
Copy after login

Browser access: linux_ip:9001.

Appendix: supervisorctl common commands

Add a new configuration file, reload

supervisorctl reread
Copy after login
Copy after login

Change a configuration file, reload

supervisorctl update
Copy after login

Restart supervisord

supervisorctl reload
Copy after login
Copy after login

View all process status

supervisorctl status
Copy after login
Copy after login

View Specify process status

supervisorctl status <name>
Copy after login

Start all child processes

supervisorctl start all
Copy after login

Start specified child processes

supervisorctl start <name>
Copy after login

Restart all child processes

supervisorctl restart all
Copy after login

Restart specified child processes

supervisorctl restart <name>
Copy after login

Stop all child processes

supervisorctl stop all
Copy after login

Stop the specified child process

supervisorctl stop <name>
Copy after login

Add a child process to the process group

supervisorctl add <name>
Copy after login

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>
Copy after login

[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!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template