当前位置:网站首页>Understanding supervisor process management

Understanding supervisor process management

2020-12-07 12:39:45 Bright brother

supervisor

Use Python Written process management program supervisor To manage Python The procedure is the most appropriate ,supervisor be based on CS framework , There are two main components :

  1. supervisord:supervisord yes supervisor The server of . Responsible for starting subroutines , Answer client command , Subroutine logging , Send event notification to process changes, etc
  2. supervisorctl: Client command line tools , You can connect to the server side , Start the process 、 close 、 restart 、 Status check, etc . The important thing is ,supervisorctl Not only can it be connected to supervisord, You can also connect to remote supervisord, Of course, on this machine, it's through UNIX socket Connected , Remote is via TCP socket Connected .supervisorctl and supervisord Communication between , It's through xml_rpc Accomplished . The corresponding configuration is in [supervisorctl] Inside the block

install

be based on ubuntu16.04, Different linux Distributions can be installed using the package manager , You can also use source installation and pip install

apt-get install python-setuptools
apt-get install supervisor

pip Installation method

pip install supervisor

Current latest version 3.3.3,supervisord.conf and supervisord.d The folder has been automatically generated in /etc/supervisor/ Under the table of contents . have access to echo_supervisord_conf Command to redirect configuration information to the specified directory , such as /etc

echo_supervisord_conf > /etc/supervisor/supervisord.conf

Server startup

supervisord -c /etc/supervisor/supervisord.conf

see supervisord Is it running

ps aux | grep supervisord

supervisord.conf Detailed explanation

Use echo_supervisord_conf see supervisord.conf Optional configuration item :

echo_supervisord_conf help

Details are as follows

; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
;  - Shell expansion ("~" or "$HOME") is not supported.  Environment
;    variables can be expanded using this syntax: "%(ENV_HOME)s".
;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".

[unix_http_server]
file=/tmp/supervisor.sock   ; socket  File path 
;chmod=0700                 ; socket  file   Pattern  ( Default  0700)
;chown=nobody:nogroup       ; socket file uid:gid owner
;username=user              ;  Use supervisorctl Connected users , By default, there are no users 
;password=123               ;  Use supervisorctl The user password of the connection , No password by default 

;[inet_http_server]         ; Web Server And remote supervisorctl  Configuration block ( Off by default )
;port=127.0.0.1:9001        ;  Listening address and port 
;username=user              ;  The logged in user , By default, there are no users 
;password=123               ;  The login password , No password by default 

[supervisord]
logfile=/tmp/supervisord.log ; supervisord Process log path 
logfile_maxbytes=50MB        ; supervisord Process single log file size , The default is 50M
logfile_backups=10           ;  Number of log files , The default is 10 individual 
loglevel=info                ;  The level of logging , The default is info, And support debug,warn,trace)
pidfile=/tmp/supervisord.pid ; supervisord Of pid File path .
nodaemon=false               ;  If it is true,supervisord The process will run in the foreground   The default is false( Background operation )
minfds=1024                  ;  This is the minimum system free file descriptor , Below this value supervisor Will not start 
minprocs=200                 ;  Minimum available process descriptors , Below this value supervisor It will not start normally 
;umask=022                   ;  The mask that the process creates the file  ( Default  022)
;user=chrism                 ; user The user specified by the parameter can also be used for supervisord Conduct management 
;identifier=supervisor       ; supervisord Identifier , Default supervisor
;directory=/tmp              ;  When supervisord When running as a daemon , start-up supervisord Before the process , Will switch to this directory first 
;nocleanup=true              ; false When  supervisord When the process starts   The temporary files generated by previous subprocesses will be removed (true Do not remove )
;childlogdir=/tmp            ;  When the child process log path is AUTO When , The storage path of the subprocess log file  ( Default  $TMP)
;environment=KEY="value"     ;  This is used to set environment variables ,supervisord stay linux By default, it inherits linux Of   environment variable , You can set supervisord Other process specific environment variables supervisord When you start a child process , The child process will copy the memory space content of the parent process .  Therefore, the environment variables set will be inherited by the child process  ( Default not set )
;strip_ansi=false            ;  The default is falsh, If set to true, Will clear all of the child process logs ANSI(\n,\t)  Sequence 

;  This option is for XML_RPC With , If you want to use supervisord perhaps web server  It has to be turned on 
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; supervisorctl Local connection supervisord, Use local UNIX socket
;serverurl=http://127.0.0.1:9001 ; supervisorctl Remote connection supervisord When , Address and port used 
;username=chris              ;  The user name of the connection login , Need and http_username Agreement 
;password=123                ;  Password to connect to login , Need and http_password Agreement 
;prompt=mysupervisor         ;  The prompt for entering the user name and password   Default :mysupervisor 
;history_file=~/.sc_history  ;  Specifies the file for historical commands 


;[program:theprogramname]      ;  Appoint program To configure 
;command=/bin/cat              ;  The process to be executed   With parameters  $1 $2 $3   Be careful !!  The executing process cannot be a daemon  ! !
;process_name=%(program_name)s ; numprocs Parameter is 1 when , Don't worry about this parameter   The default value is %(program_name)s That's the one on the top program The name after the colon 
;numprocs=1                    ;  Number of startup processes . When not for 1 when , It's the concept of process pools , The default is 1
;directory=/tmp                ;  Before the process runs , Switch to this directory before the meeting 
;umask=022                     ;  Process mask  (default None)
;priority=999                  ;  Subprocess startup and shutdown priority , Low priority , Start first , When it's closed, it's last closed  (default 999)
;autostart=true                ;  Set to true  The child process will be in supervisord It is automatically started after starting , The default is true
;startsecs=1                   ;  Set the number of seconds after the subprocess starts , At this point, if the state is running, Then we think the launch is successful 
;startretries=3                ;  After the process failed to start , Maximum number of attempts to start   When more than 3 Next time ,supervisor The status of this process will be set to FAIL
;autorestart=unexpected        ;  Set the automatic restart of the child process after hanging up , There are three options ,false,unexpected and true. If false When , In any case , Will not be restarted , If unexpected, Only when the exit code of the process is not on it exitcodes When the exit code defined in it ,> Will be automatically restarted . When it comes to true When , As long as the child process crashes , Will be rebooted unconditionally . The default is unexpected
;exitcodes=0,2                 ;  Pay attention to the above autorestart=unexpected Corresponding  exitcodes The exit code defined in it is expected Of .
;stopsignal=QUIT               ;  Process stop signal , It can be for TERM, HUP, INT, QUIT, KILL, USR1, or USR2 Equal signal   The default is TERM  When using the set signal to kill the process , The exit code will be considered expected
;stopwaitsecs=10               ;  This is when we send stopsignal After the signal , Return information to the system supervisord, The maximum time waiting .  Beyond that time ,supervisord A force... Is sent to the subprocess kill The signal of ( Default 10 second )
;stopasgroup=false             ;  This thing is mainly used for ,supervisord Managed subprocesses , This subprocess itself has subprocesses   So if we just kill supervisord Of the sub processes of , The child process of a child process may become an orphan process   So we can set this option , Kill the entire process group of the entire subprocess    Set to true Words , commonly killasgroup It will also be set to true  This option sends stop The signal (def false)
;killasgroup=false             ;  This and the above stopasgroup similar , But it was sent kill The signal (def false)
;user=chrism                   ;  If supervisord yes root start-up , Let's set this non here root user , Can be used to manage the program  Default not set 
;redirect_stderr=true          ;  by true, be stderr Will be written to stdout Log file  (default false)
;stdout_logfile=/a/path        ;  Subprocess's stdout Log path of , You can specify a path ,AUTO,none Wait for three options   Set to none Words , No logs will be generated . Set to AUTO Words , Will randomly find a place to make a log file , And when supervisord When you restart , Previous log files will be emptied . When  redirect_stderr=true When ,sterr It will also be written into this log file 
;stdout_logfile_maxbytes=1MB   ;  Maximum log file size , and [supervisord] As defined in  (default 50MB)
;stdout_logfile_backups=10     ;  and [supervisord] The definition is the same  (0 means none, default 10)
;stdout_capture_maxbytes=1MB   ;  This thing is set up capture The size of the pipe , It's not worth it 0 When , Subprocesses can be downloaded from stdout Send a message , and supervisor According to the information , Send the corresponding event  (default 0)
;stdout_events_enabled=false   ;  by ture When , When a child process is made by stdout When writing a log to a file descriptor , Will trigger supervisord send out PROCESS_LOG_STDOUT Type of event(default false)
;stderr_logfile=/a/path        ;  Set up stderr Write log path , When redirect_stderr=true. There is no need to set this , It's no use setting it . Because it's going to be written stdout_logfile In the same file  default AUTO( Find a place to store ,supervisord Restart is cleared )
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ;  This is the environment variable of the subprocess , It is not shared with other child processes 
;serverurl=AUTO                ; override serverurl computation (childutils)

;[eventlistener:theeventlistenername]    ; eventlistener Actually sum program It's the same status , It's also suopervisor The child process started , But what it does is subscribe supervisord Sent event. His name is listener 了 . We can do it in listener There's a series of processing inside , For example, call the police ....
;command=/bin/eventlistener    ;  And the above program equally , Express listener The path to the executable of 
;process_name=%(program_name)s ;  This one is the same , Process name , When the following numprocs For many times , That's what we need . Otherwise, by default OK 了 
;numprocs=1                    ;  same listener The number of starts , Default 1
;events=EVENT                  ; event event Type of event , in other words , Only the types of events written in this place . Will be sent 
;buffer_size=10                ; event Queue cache size  (default 10)
;directory=/tmp                ;  Before the process is executed , Will switch to this directory to execute  (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ;  boot priority  (default -1)
;autostart=true                ; supervisord Start together when you start  (default: true)
;startsecs=1                   ;  Set the number of seconds after the subprocess starts , At this point, if the state is running, Then we think the launch was successful  (def. 1)
;startretries=3                ;  The maximum number of failed attempts  (default 3)
;autorestart=unexpected        ;  and program equally  (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=false         ; redirect_stderr=true is not allowed for eventlisteners
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

;[group:thegroupname]          ;  to programs grouping , Divided into groups program. We don't have to operate one by one   We can operate group names uniformly .  Be careful :program After being divided into groups , From the original configuration supervisor Disappeared from the configuration file of supervisor Only manage groups , It's no longer about the individual in the group program Managed 
;programs=progname1,progname2  ;  The group members , Comma off 
;priority=999                  ;  priority , Relative to and between groups  (default 999)

;[include]                     ; Included program configuration files 
;files = relative/directory/*.ini

extract supervisord.conf Templates

use supervisord.conf and program.conf Separate the way of native process management , Extract the commonly used supervisord Templates

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)

[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)
username=erow              ; (default is no username (open server))
password=j                 ; (default is no password (open server))

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
loglevel=debug               ; (log level;default info; others: debug,warn,trace)
logfile_maxbytes=10MB        ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10           ; (num of main logfile rotation backups;default 10)

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

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

[include]
files = /etc/supervisor/conf.d/*.ini

Template extraction is based on the following considerations

  • Required options are required
  • The server supports local supervisorctl Client connections also support remote connections
  • The test environment log level is set to debug, The production environment log level is set to info, modify loglevel Field can be
  • The size and number of log files need to refer to the disk size of the server , Will produce stderr and stdout Two kinds of logs
  • pid Document and log Documents are not recommended to be stored in /tmp below , Easily deleted by mistake , So put it in var Under the table of contents

extract program.conf Templates

Extract the commonly used program Templates

[program:concurrency_statistician]
directory = /root/qk_python/data/statistic/concurrency_statistician
command = python concurrency_statistician.py
autostart = true
startsecs = 5
autorestart = true
startretries = 3
user = root

If you adjust it separately program Log size and number , Join in stdout and stderr Four parameters , The new configuration file is as follows

[program:concurrency_statistician]
directory = /root/qk_python/data/statistic/concurrency_statistician
command = python concurrency_statistician.py
autostart = true
startsecs = 5
autorestart = true
startretries = 3
user = root
stdout_logfile_maxbytes = 10MB
stdout_logfile_backups = 10
stderr_logfile_maxbytes = 10MB
stderr_logfile_backups = 10

In general , There is no need to specify the log file name , The default log file name group is

  • Program name + Log type (stdout and stderr)+ supervisor+ Process number + Log number

Example file name :concurrency_statistician-stderr---supervisor-OpZ5di.log.2

We need to pay attention to :supervisor Server program supervisord After reboot , There will be new pid. Therefore, the log will only be generated in the new process log , Old child process logs will not be deleted . So if you need to restart supervisord, You need to pay attention to whether to keep the old process log , To avoid exceeding the disk size .

In general , In a production environment, it doesn't always restart supervisord

Commonly used supervisorctl command

Can enter the supervisorctl Of shell Interface , You can also directly bash Terminal operation

#  Stop a process ,program_name  by  [program:x]  Inside  x
supervisorctl stop program_name
#  Start a process 
supervisorctl start program_name
#  Restart a process 
supervisorctl restart program_name
#  Stop the whole process , notes :start、restart、stop  Will not load the latest configuration file 
supervisorctl stop all
#  Load the latest configuration file , Stop the original process and start with the new configuration 、 Manage all processes 
supervisorctl reload
#  According to the latest profile , Start a new configuration or a process with changes , Processes with no changes to the configuration will not be affected and will be restarted 
supervisorctl update

About program groups The command used less, will not introduce


Reference material :

  1. supervisor Official documents
  2. Xiao Qiang Zhai is too -Supervisor brief introduction 、 install 、 To configure
  3. Use supervisor Management process
  4. Liao Xuefeng -Linux Background process management tools :supervisor
  5. Tortoise operation and maintenance -Supervisor Installation and configuration
  6. oschina-SUPERVISOR Process Manager Configuration Guide

Remember to give me some praise !

Carefully organized the computer in all directions from the entry 、 Advanced 、 Practical video courses and e-books , Classify according to the catalogue , Always find the learning materials you need , What are you waiting for ? Pay attention to downloads !!!

resource-introduce

Not forget for a moment , There must be an echo , Guys, please give me a compliment , Thank you very much .

I'm a bright brother in the workplace ,YY Senior software engineer 、 Four years working experience , The slash programmer who refuses to be the leader of salted fish .

Listen to me , More progress , Program life is a shuttle

If I'm lucky enough to help you , Please order one for me 【 Fabulous 】, Pay attention , If you can give me a little encouragement with your comments , Thank you very much .

A list of articles by Liang Ge in the workplace : More articles

wechat-platform-guide-attention

All my articles 、 The answers are all in cooperation with the copyright protection platform , The copyright belongs to brother Liang , unaccredited , Reprint must be investigated !

版权声明
本文为[Bright brother]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207123635936w.html