Simple use of supervisor

(1) supervisor installation

(1) ubuntu installation:

apt-get install supervisor

(2) centos installation:

yum install supervisor

(3) pip installation:

pip install supervisor

ps: configuration file address: / etc / Supervisor / Supervisor Conf, the annotation in the configuration file is * *** Not#

(2) Structure of supervisor

After your installation is successful, a configuration file (supervisor. CONF) and a folder conf.d will be generated in the / etc/supervisor / directory respectively

(1) Supervisor Conf configuration file:

supervisord. The conf file is generally composed of the following six parts

(1) [unix_http_server]:

This part is generally configured in this way

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

(2) [inet_http_server]:

[inet_http_server] ; HTTP Server, providing web Management interface
port=0.0.0.0:9001 ; Web Manage background running IP And ports. If you open to the public network, you need to pay attention to security
username=root ; User name of login management background
password=123 ; Password for login management background

(3) [supervisor]:

This part is generally configured in this way

[supervisord]
logfile = /tmp/supervisord.log            # Log path
logfile_maxbytes = 50MB                   # The maximum number of bytes that may be consumed before log rotation. 0 represents unlimited log size
logfile_backups=10                        # The number of backups caused by log rotation. 0 means no backup is reserved
loglevel = info                           # Log level (log level, including critical, error, warn, info, debug, trace, or blather, default to info... Non mandatory)
pidfile = /tmp/supervisord.pid            # Keep the location of its pid file
nodaemon = false                          # If true, supervisor will start in the foreground instead of the daemon. Default false
minfds = 1024                             # The minimum number of file descriptors that must be available before supervisor will start successfully. The default is 1024
minprocs = 200                            # The minimum number of process descriptors that must be available before supervisor will start successfully. The default is 200
umask = 022                               # umask of super process. Default 022
user = chrism                             # Instructs supervisor to switch the user to this UNIX user account before any meaningful processing. By default, the user is not switched
identifier = supervisor                   # The identifier string of this supervisor process used by the RPC interface. The default is supervisor
directory = /tmp                          # When the supervisor daemon, switch to this directory
nocleanup = true                          # Prevent supervisor from clearing any existing AUTO sub log files at startup. The default is false
childlogdir = /tmp                        # Directory for AUTO sub log files.
strip_ansi = false                        # Strip all ANSI escape sequences from the child log file
environment = KEY1="value1",KEY2="value2" # A list of key / value pairs in the form of KEY="val",KEY2="val2" that will be placed in the supervisor process environment (and as a result of the environment of all its child processes).

(4) [supervisorctl]:

[supervisorctl]
serverurl=unix:////var/run/supervisor.sock ;  This is the path of the local UNIX socket when the supervisorctl connects to the supervisord locally. Note that this is corresponding to the previous [unix_http_server]. The default value is UNIX: var / run / Supervisor sock. .  Not required

#serverurl=http://127.0.0.1:9001  ;  This is the TCP socket path used when the supervisorctl connects to the supervisord remotely. Note that this corresponds to the previous [inet_http_server]. The default is http://0.0.0.0:9001 . . .  Non mandatory

username = root                        # User name passed to the supervisor server for authentication (empty by default. Not required)
password = 123                          # Password to pass to the supervisor server for authentication (empty by default. Not required)
prompt = mysupervisor                   # A string used as a supervisor CTL prompt
history_file = /tmp/his_file.log        # The path used as the readline persistence history file

(5) [rpcininterface: Supervisor]:

This is the default configuration here

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

(6) [include]:

[include]
files = /etc/supervisor/conf.d/*.conf #Manage all processes in this directory

(2) Folder conf.d:

This directory is used to store supervisor The configuration file of the include node under conf (usually an independent configuration file for each independent process)

The document structure is as follows:

[program:Project name]  #It is the unique identification of the application and cannot be repeated
directory = /home/user/project  #Process directory
priority=999                  # The relative priority of the program in the startup and shutdown sequence. The higher the number, the higher the priority
command = python xxx.py  #Commands for executing programs
autorestart = true  #Whether to restart automatically
user = user  #Started user
redirect_stderr = true  #Redirect stderr to stdout. The default is false
stdout_logfile = stdout.log  #Exception log
stderr_logfile = stderr.log  #Normal log
loglevel = info  #log level

#numprocs=10 ; Number of processes, note: (equivalent to executing 10 command commands)
#process_name=%(program_name)s_%(process_num)02d


There are two parameters to note here. The number of numprocs is the number of times the command is executed. The default is 1 and process_name defaults to the project name, but if your numprocs parameter is not only 1, then process_name cannot be a fixed value because process_name cannot be repeated, and you will find that it is usually output to stderr_ Directory corresponding to logfile.

(3) Configuration instance

Here is supervisor There is no need to modify the conf configuration file. We use supervisor to maintain the process of uwsgi. The configuration file is as follows

[program:uwsgi]


priority=5
autostart=true
autorestart=true
startsecs=5  #If the process is normal within 5 seconds of startup, it is regarded as normal startup
startretries=1  #The number of attempts to reply after a process exception
user=root
directory=/home/djangoProject/

#Because anaconda virtual environment is needed here
command =bash -c "source activate && conda activate my_paddle && cd /home/djangoProject/ && uwsgi uwsgi.ini"
 
stdout_logfile=/home/djangoProject/uwsgi.log 
stderr_logfile=/home/djangoProject/uwsgi.log

In addition, it is better not to set background enabling in the uwsgi configuration file, otherwise the supervisor will think that uwsgi has not been started successfully

uwsgi configuration file instance

[uwsgi]
#Start: uwsgi ini
#Stop: uwsgi -- stop uwsgi pid
socket = 127.0.0.1:8080
chdir = /home/djangoProject
wsgi-file = djangoProject/wsgi.py
processes = 1
threads = 4
pidfile=uwsgi.pid

#Run in the background and output the log
#daemonize = /home/djangoProject/uwsgi.log 

(4) web management and peripheral commands

web management, enter ip:9001 in the browser

Peripheral commands:
supervisord -c /etc/supervisor/supervisord.conf foreground start
supervisord -n -c /etc/supervisor/supervisord.conf background startup

supervisorctl stop program_name stops a process
supervisorctl start program_name starts a process
supervisorctl restart program_name restarts a process
supervisorctl stop all stops all processes
supervisorctl reload loads the latest configuration file, stops the original process, and starts and manages all processes according to the new configuration
supervisorctl update starts the process with new configuration or changes according to the latest configuration file. The process without configuration changes will not be affected and restarted

(5) Conclusion

Supplement:
https://blog.csdn.net/wc1695040842/article/details/103988813
https://blog.csdn.net/BLUE5945/article/details/80119248

If there are any mistakes, please criticize and correct them. Finally, I hope all my friends can gain something. It's not easy to code. If you like it, please pay attention to the wave. Let's go

Keywords: Python supervisor

Added by thegame261 on Wed, 09 Feb 2022 15:25:34 +0200