[Linux] Supervisor usage details

[Linux] Supervisor usage details

1, Introduction to supervisor

Supervisor is a general process management program developed in Python. It can change an ordinary command-line process into a background daemon, monitor the process status, and automatically restart in case of abnormal exit. It starts these managed processes as child processes of the supervisor through fork/exec, so as long as the path of the executable file of the process to be managed is written in the supervisor configuration file. It also realizes that when the child process hangs, the parent process can accurately obtain the hanging information of the child process, and can choose whether to start and alarm by itself. Supervisor also provides a function to set a non root user for supervisor or each child process, and this user can manage its corresponding process.

2, supervisor installation

After configuring the yum source, you can install it directly

yum install supervisor

Debian/Ubuntu can be installed via apt

apt-get install supervisor

pip installation

pip install supervisor

easy_install install

easy_install supervisor

3, supervisor use

Supervisor configuration file: / etc / supervisor.conf

Note: the configuration file of supervisor is incomplete by default, but in most cases, the basic functions mentioned above have been met by default.

Child process configuration file path: / etc / Supervisor. D/

Note: the default subprocess configuration file is in ini format, which can be modified in the supervisor main configuration file.

4, Profile description

supervisor.conf configuration file description:

[unix_http_server]
file=/tmp/supervisor.sock   ;UNIX socket Documents, supervisorctl Can use
;chmod=0700                 ;socket Document mode,The default is 0700
;chown=nobody:nogroup       ;socket Document owner,Format: uid:gid
 
;[inet_http_server]         ;HTTP Server, providing web Management interface
;port=127.0.0.1:9001        ;Web Manage background running IP And ports. If you open to the public network, you need to pay attention to security
;username=user              ;User name of login management background
;password=123               ;Password for login management background
 
[supervisord]
logfile=/tmp/supervisord.log ;Log file, default is $CWD/supervisord.log
logfile_maxbytes=50MB        ;Log file size, exceeding rotate,Default 50 MB,If set to 0, it means that the size is not limited
logfile_backups=10           ;The number of reserved backups of log files is 10 by default. If it is set to 0, it means no backup
loglevel=info                ;Log level, default info,other: debug,warn,trace
pidfile=/tmp/supervisord.pid ;pid file
nodaemon=false               ;Whether to start in the foreground. The default is false,Namely daemon Start by
minfds=1024                  ;The minimum value of the file descriptor that can be opened. The default value is 1024
minprocs=200                 ;The minimum number of processes that can be opened. The default is 200
 
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; Connect supervisor through UNIX socket, and the path is the same as UNIX_ http_ The file of the server part is consistent
;serverurl=http://127.0.0.1:9001 ;  Connect to supervisor via HTTP
 
; [program:xx]Is the configuration parameter of the managed process, xx Is the name of the process
[program:xx]
command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run  ; Program start command
autostart=true       ; stay supervisord It also starts automatically when starting
startsecs=10         ; If there is no abnormal exit after 10 seconds of startup, it means that the process is started normally. The default is 1 second
autorestart=true     ; Automatic restart after program exit,Optional values:[unexpected,true,false],Default to unexpected,Indicates that the process was restarted after being killed unexpectedly
startretries=3       ; The number of automatic retries after startup failure. The default is 3
user=tomcat          ; Which user is used to start the process? The default is root
priority=999         ; The process startup priority is 999 by default. The process with a small value will be started first
redirect_stderr=true ; hold stderr Redirect to stdout,default false
stdout_logfile_maxbytes=20MB  ; stdout Log file size, default 50 MB
stdout_logfile_backups = 20   ; stdout The number of log file backups. The default is 10
; stdout The log file cannot be started normally when the specified directory does not exist, so you need to create the directory manually( supervisord (log files are created automatically)
stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
stopasgroup=false     ;Default to false,Whether to send to this process group when the process is killed stop Signals, including child processes
killasgroup=false     ;Default to false,Send to process group kill Signals, including child processes
 
;Include other configuration files
[include]
files = relative/directory/*.ini    ;You can specify one or more to.ini End profile

Description of sub process configuration file:

Write a configuration file for the sub process (program) to be managed and put it in the / etc/supervisor.d / directory with. ini as the extension (the configuration file of each process can be separated separately or the relevant scripts can be put together). For example, arbitrarily define an option group of project name related to script (/ etc/supervisor.d / test. CONF):

#Project name
[program:blog]
#Script directory
directory=/opt/bin
#Script execution command
command=/usr/bin/python /opt/bin/test.py

#Whether the supervisor is started at the same time when it is started. The default is True
autostart=true
#When the program exits, the program will not restart automatically. The default is unexpected. Set the automatic restart after the subprocess hangs up. There are three options: false,unexpected and true. If it is false, it will not be restarted under any circumstances. If it is unexpected, it is only when the exit code of the process is not defined in the following exit codes
autorestart=false
#This option is the number of seconds after the child process is started. If the status is running, we think it has been started successfully. The default value is 1
startsecs=1

#User identity of the script to run 
user = test

#Log output 
stderr_logfile=/tmp/blog_stderr.log 
stdout_logfile=/tmp/blog_stdout.log 
#Redirect stderr to stdout. The default is false
redirect_stderr = true
#stdout log file size, 50MB by default
stdout_logfile_maxbytes = 20MB
#Number of stdout log file backups
stdout_logfile_backups = 20

Sub process configuration example:

#The description is the same as above
[program:test] 
directory=/opt/bin 
command=/opt/bin/test
autostart=true 
autorestart=false 
stderr_logfile=/tmp/test_stderr.log 
stdout_logfile=/tmp/test_stdout.log 
#user = test  

5, supervisor command description

Common commands

supervisorctl status        //View the status of all processes
supervisorctl stop es       //Stop es
supervisorctl start es      //Start es
supervisorctl restart       //Restart es
supervisorctl update        //After the configuration file is modified, use this command to load a new configuration
supervisorctl reload        //Restart all programs in the configuration

Note: changing es to all can manage all processes in the configuration. Directly enter supervisorctl to enter the shell interaction interface of supervisorctl. At this time, the above commands can be used directly without supervisorctl.

matters needing attention

Start the supervisor before using the supervisor process management command, otherwise the program will report an error.
Start with the command Supervisor - C / etc / supervisor.conf.
If centos7:

systemctl start supervisord.service     //Start the supervisor and load the default configuration file
systemctl enable supervisord.service    //Add supervisor to boot entry

common problem

  • unix:///var/run/supervisor.sock no such file
    Problem Description: the error reported by using the supervisorctl directly without starting the service after installing the supervisor
    Solution: Supervisor - C / etc / supervisor.conf

  • The process specified in the command has been started, but the supervisor keeps restarting
    Problem Description: in the command, the startup mode is background startup, resulting in no pid recognition, and then restart continuously. Elasticsearch is used here, and $path/bin/elasticsearch -d is specified in the command
    Solution: the supervisor cannot detect the pid of the background startup process, and the supervisor itself is the background startup daemon, so don't worry about this

  • Multiple supervisor services were started, resulting in failure to shut down the service normally
    Problem Description: Run Supervisor - C / etc / supervisor.d/xx.conf directly before running Supervisor - C / etc / supervisor.conf, resulting in some processes being managed by multiple supervisors and unable to shut down processes normally.
    Solution: use PS - Fe | grep supervisor to view all started supervisor services and kill related processes.

Keywords: Python Linux Operation & Maintenance

Added by Kawal on Wed, 27 Oct 2021 02:52:03 +0300