Supervisor is defined as super user and Supervisor on Baidu Encyclopedia. Supervisor is a process management tool that automatically restarts a process when it is interrupted. It can run on various Unix-like machines. Supervisor is a universal process manager developed in Python. It can change a common command line process into a background daemon, monitor the process status, and automatically restart upon abnormal exit.

V introduces the Supervisor

– supervisord

When running the Supervisor, it launches a process it supervises, which it is responsible for launching the process it manages, running it as its own child process, and is able to restart automatically if the managed process crashes.

– supervisorctl

The command line management tool can be used to run the stop, start, and restart commands to manage these subprocesses.

As the parent process of all processes, the Supervisor manages the progress of the child process. The Supervisor manages the child process with the PID of the child process. When the child process exits unexpectedly, the Supervisor can receive the corresponding semaphore.

V installation Supervisor

♛ Installation method 1

1.0.0 install python

yum install python-setuptools

The installation Supervisor 1.0.1

easy_install supervisor

♛ Installation method 2

Wget https://pypi.python.org/packages/source/s/supervisor/supervisor-3.1.3.tar.gz tar ZXVF supervisor - 3.1.3. Tar. Gz CD The supervisor - 3.1.3 python setup. Py installCopy the code

I used the installation method 2 to install,

1.2 Checking the Installation is successful

echo_supervisord_conf

V configuration of the Supervisor

2.0 Create a directory and initialize the configuration file

mkdir /usr/supervisor
echo_supervisord_conf > /usr/supervisor/supervisord.conf
Copy the code

 

Echo_supervisord_conf a:Copy the code

[root@centos-011 ~ 07:50:00]#cat /etc/supervisord.conf.bak
; Sample supervisor config file.
 
[unix_http_server]
file=/var/run/supervisor/supervisor.sock   ; socket 路径
 
;chmod=0700                 ; socket 文件的权限
;chown=nobody:nogroup       ; socket 所属用户及组
;username=user              ; 用户名
;password=123               ; 密码
 
;[inet_http_server]         ; 是否启用服务,默认是关闭的(启用的话可以看到supervisor 管理的服务状态)
;port=127.0.0.1:9001        ; 监听的IP及端口
;username=user              ; 用户名
;password=123               ; 密码
 
[supervisord]               ; supervisord 全局配置
logfile=/var/log/supervisor/supervisord.log  ; supervisor 日志路径
logfile_maxbytes=50MB       ; 单个日志文件最大数
logfile_backups=10          ; 保留多少个日志文件(默认10个)
loglevel=info               ; (log level;default info; others: debug,warn,trace)
pidfile=/var/run/supervisord.pid ; pid 文件路径
nodaemon=false              ; 启动是否丢到前台,设置为false ,表示以daemon 的方式启动
minfds=1024                 ; 最小文件打开数,对应系统limit.conf 中的nofile ,默认最小为1024,最大为4096
minprocs=200                ; 最小的进程打开数,对应系统的limit.conf 中的nproc,默认为200
;umask=022                  ; (process file creation umask;default 022)
;user=chrism                 ; 启动supervisord 服务的用户,默认为root
;identifier=supervisor       ; (supervisord identifier, default is 'supervisor')
;directory=/tmp              ; 这里的目录指的是服务的工作目录
;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value       ; (key value pairs to add to environment)
;strip_ansi=false            ; (strip ansi escape codes in logs; def. false)
 
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
 
[supervisorctl]
serverurl=unix:///var/run/supervisor/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available
 
; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.
 
;[program:theprogramname]      ; 定义一个守护进程 ,比如下面的elasticsearch 
;command=/bin/cat              ; 启动程序使用的命令,可以是绝对路径或者相对路径
;process_name=%(program_name)s ; 一个python字符串表达式,用来表示supervisor进程启动的这个的名称,默认值是%(program_name)s
;numprocs=1                    ; Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须包含%(process_num)s,默认是1
;directory=/tmp                ; supervisord在生成子进程的时候会切换到该目录
;umask=022                     ; umask for process (default None)
;priority=999                  ; 权重,可以控制程序启动和关闭时的顺序,权重越低:越早启动,越晚关闭。默认值是999
;autostart=true                ; 如果设置为true,当supervisord启动的时候,进程会自动启动
;autorestart=true              ; 设置为随 supervisord 重启而重启,值可以是false、true、unexpected。false:进程不会自动重启
;startsecs=10                  ; 程序启动后等待多长时间后才认为程序启动成功,默认是10秒
;startretries=3                ; supervisord尝试启动一个程序时尝试的次数。默认是3
;exitcodes=0,2                 ; 一个预期的退出返回码,默认是0,2。
;stopsignal=QUIT               ; 当收到stop请求的时候,发送信号给程序,默认是TERM信号,也可以是 HUP, INT, QUIT, KILL, USR1, or USR2
;stopwaitsecs=10               ; 在操作系统给supervisord发送SIGCHILD信号时等待的时间
;user=chrism                   ; 如果supervisord以root运行,则会使用这个设置用户启动子程序
;redirect_stderr=true          ; 如果设置为true,进程则会把标准错误输出到supervisord后台的标准输出文件描述符
;stdout_logfile=/a/path        ; 把进程的标准输出写入文件中,如果stdout_logfile没有设置或者设置为AUTO,则supervisor会自动选择一个文件位置
;stdout_logfile_maxbytes=1MB   ; 标准输出log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小
;stdout_logfile_backups=10     ; 标准输出日志轮转备份的数量,默认是10,如果设置为0,则不备份
;stdout_capture_maxbytes=1MB   ; 当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB
;stdout_events_enabled=false   ; 如果设置为true,当进程在写它的stderr
;stderr_logfile=/a/path        ; 把进程的错误日志输出一个文件中,除非redirect_stderr参数被设置为true
;stderr_logfile_maxbytes=1MB   ; 错误log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小
;stderr_logfile_backups=10     ; 错误日志轮转备份的数量,默认是10,如果设置为0,则不备份
;stderr_capture_maxbytes=1MB   ; 当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB
;stderr_events_enabled=false   ; 如果设置为true,当进程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发
;environment=A=1,B=2           ; 一个k/v对的list列表
;serverurl=AUTO                ; 是否允许子进程和内部的HTTP服务通讯,如果设置为AUTO,supervisor会自动的构造一个url
 
; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.
 #这个地方是自定义一个守护进程
[program:elasticsearch]                       ; 定义一个守护进程 elasticsearch
environment=ES_HOME=/usr/local/elasticsearch  ; 设置ES_HOME 环境变量
user=elk                                      ; 启动elasticsearch 的用户
directory=/usr/local/elasticsearch            ; 进入到这个目录中
command=/usr/local/elasticsearch/bin/elasticsearch ; 执行启动命令
numprocs=1                                    ; Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须包含%(process_num)s,默认是1
autostart=true                                ; 设置为随 supervisord 启动而启动
autorestart=true                              ; 设置为随 supervisord 重启而重启
startretries=3                                ; 设置elasticsearch 重启的重试次数
priority=1                                    ; 权重,可以控制程序启动和关闭时的顺序,权重越低:越早启动,越晚关闭。默认值是999  
 
;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; start at supervisord start (default: true)
;autorestart=unexpected        ; restart at unexpected quit (default: unexpected)
;startsecs=10                  ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;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        ; # 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)
 
; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.
 
;[group:thegroupname]          ; 服务组管理,可以将多个服务名写到这里管理(组名自定义)
;programs=progname1,progname2  ; 上面配置好的服务名,比如elasticsearch,kibana,logstash
;priority=999                  ; the relative start priority (default 999)
 
; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.
 
[include]
files = supervisord.d/*.ini
Copy the code

View Code

 

2.1 Create a folder to avoid storing all new configuration information in a configuration file

mkdir /usr/supervisor/supervisord.d/

2.2 Modifying the System Configuration File

vim /usr/supervisor/supervisord.conf

The new container in 2.3 include 2.1 is designed to containall conf files in the folder it is created in

[include]
files = /usr/supervisor/supervisord.d/*.conf
Copy the code

2.4 Remove the comment on [inet_http_server] if you need to view processes on the Web UI

[inet_http_server] port = 127.0.0.1:9001; IP Username =user password=123Copy the code

V operation Supervisor

3.0 start the supervisord

supervisord -c /usr/supervisor/supervisord.conf

3.1 Viewing Listeners

“lsof -i:9001

At this point, you can view the Supervisor Web interface at http://ip:9001/, although no monitors have been added yet.

3.2 Add a configuration file that the Supervisor uses to monitor the program

cat > /usr/supervisor/supervisord.d/supervisor_test_one.conf

The following configuration is added to the new supervisor_test_one.conf file

[program:test_one] command=java -jar /data/smallvideo/supervisor/taskApp-exec.jar TaskTestOne ; Monitored process path priority=1. The higher the number, the higher the priority numprocs=1. Start several processes autostart=true; While the container is running, the autorestart=true; Automatic restart Startretries =10; Exitcodes =0; exitCodes =0; Normal exit code stopsignal=KILL; Stopwaitsecs =10; Wait time before sending SIGKILL redirect_stderr=true; To redirect stderr to stdout [program: test_two] command = Java - jar/data/smallvideo/supervisor/taskApp - exec. Jar TaskTestTwo; Monitored process path priority=1. The higher the number, the higher the priority numprocs=1. Start several processes autostart=true; While the container is running, the autorestart=true; Automatic restart Startretries =10; Exitcodes =0; exitCodes =0; Normal exit code stopsignal=KILL; Stopwaitsecs =10; Wait time before sending SIGKILL redirect_stderr=true; Redirect stderr to stdoutCopy the code

3.3 Supervisor Configuration File Description

-command: command used to start the program, either absolute path or relative path -process_name: A Python string expression that represents the name of the supervisor process to start. The default value is %(program_name) s-numprocs: The Supervisor starts multiple instances of this program, and if numprocs>1, the expression for process_name must contain %(process_num)s, which defaults to 1 - numprocs_start: An int offset used to calculate the numprocs value when an instance is started. -priority: weight controls the order in which programs are started and closed. The lower the weight, the earlier they are started and the later they are closed. The default is 999 - autostart: if set to true, the process is rebooted automatically when the container is running. - autorestart: the value can be false, true, or unexpected. Unexpected: The process will restart when the program exits with an exitcode other than that defined in exitCodes. True: the process will restart unconditionally when exiting. - startSecs: indicates the length of time after the program is started before it is deemed to be successfully started. - Startretries: supervisord Indicates the number of times that a program is tried. Default is 3 - exitcodes: an expected exit return code, which defaults to 0,2. - stopSignal: when a stop request is received, a signal is sent to the program. The default signal is TERM signal, which can also be HUP, INT, QUIT, KILL, USR1, or USR2. -stopWaitSecs: the time it takes to wait while the operating system is sending SIGCHILD signals to the container. -stopasGroup: if set to true, the Supervisor will send stop signals to the entire process group. -killasGroup: If set to true, the SIGKILL signal sent to the program will be sent to the entire process group, and its children will be affected. - redirect_stderr: when set to true, the process prints standard errors to the standard output file descriptor in the background container that is running in the container. - stdout_logFILE: Writes the standard output of the process to a file. If stdout_logfile is not set or is set to AUTO, the Supervisor automatically selects a file location. - stdout_logFILe_maxbytes: the system automatically rotates the number of standard output log files. The unit is KB, MB, or GB. -stdout_logFILe_backups: Number of standard output log backups in rotation. The default value is 10. If the parameter is set to 0, no backups are required. The maximum number of bytes that can be written to the FIFO queue when the process is in stderr Capture mode. The unit can be KB, MB, or GB-stdout_EVENTS_enabled: If set to true, the PROCESS_LOG_STDERR event is emitted when the process writes its stderr to the file descriptor - stderr_logfile: Output process error logs to a file unless redirect_stderr is set to true - stderr_logFILe_maxbytes: The error logfile is automatically rotated to the number of bytes in KB, MB, or GB. -stderr_logFILe_backups: Number of rotation backups of error logs. The default value is 10. If this parameter is set to 0, no backups are required. The maximum number of bytes that can be written to the FIFO queue when the process is in stderr Capture mode. The unit can be KB, MB, or GB-stderr_EVENTS_enabled: If set to true, PROCESS_LOG_STDERR events will be triggered when the process writes its stderr to the file descriptor - environment: a list of K/V pairs - directory: It is designed to enable the process to communicate with the internal HTTP server. If it is set to AUTO, the Supervisor will automatically construct a URLCopy the code

View Code

Example monitoring demotest program:

[program:demotest] command=python demotest.py 10000 ; Run the directory=/root/ command to start monitored processes. Priority =1; priority=1; The higher the number, the higher the priority numprocs=1. Start several processes autostart=true; While the container is running, the autorestart=true; Automatic restart. Of course you have to choose Startretries =10; Exitcodes =0; exitCodes =0; The normal exit code does not restart when the exit code is this? To be determined) stopsignal=KILL; Stopwaitsecs =10; Wait time before sending SIGKILL redirect_stderr=true; Redirect stderr to stdoutCopy the code

3.4 restart the supervisor

supervisorctl -c /usr/supervisor/supervisord.conf

You can now access the test_one program at http://ip:9001/.

3.5 Supervisor Automatically starts after startup

3.5.1 In the /usr/lib/systemd/system/bin/container, create a new file supervisord.service and add the configuration

[Unit] Description=Process Monitoring and Control Daemon After=rc-local.service nss-user-lookup.target [Service] Type=forking ExecStart=/usr/bin/supervisord -c /usr/supervisor/supervisord.conf ; Run ExecStop=/usr/bin/supervisord shutdown ExecReload=/usr/bin/supervisord reload killMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.targetCopy the code

3.5.2 Starting the Service

systemctl enable supervisord

3.5.3 Verify whether it is started after startup

systemctl is-enabled supervisord

VSupervisor surrounding

4.1 Supervisor Common Commands

### the container is designed to handle commands when the container is run (type help <topic>): ===================================== add exit open reload restart start tail avail fg pid remove shutdown status update Clear maintail quit reread Signal Stop version ### is the running process list # supervisorCTL statusCopy the code

The update is designed to update the new configuration to the supervisord.

Reload, all configuration files are loaded, and all processes are started and managed according to the new configuration (which will restart the previously running program)

Start XXX: starts a process

Restart XXX: restarts a process

Stop XXX: stops a process (XXX). XXX is the value configured in [Program: theprogramName]

Stop groupworker: Restart all processes belonging to groupworker (same as start and restart)

Stop all: Stops all processes. Note: start, restart, and stop do not load the latest configuration file

Reread, which is executed when a service is changed from automatic to manual startup

Ps: some versions do not support the supervisorctl, can pass supervisorctl – c/usr/supervisor/supervisord. Conf call command, format as above, the actual results are as follows.

4.2 If the CPU and memory usage is particularly high, it will cause the machine to jam.

Try commenting out the Web in 2.4.

Github.com/Supervisor/…

4.3 Unlinking stale socket/TMP /supervisor. Sock

For the solution, run unlink/TMP /supervisor.sock

 

About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \