博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Linux基础 - 服务管理 supervisor

Posted on 2023-02-19 20:35  Kingdomer  阅读(89)  评论(0编辑  收藏  举报

 

一、supervisor 

1.1 supervisor 介绍 

Supervisor 是一个进程管理工具, 当进程中断的时候S upervisor能自动重新启动它, 可以运行在各种 类unix系统上。

Supervisor 是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。

 

1.2 supervisor 安装

# 1. 使用epel 仓库进行安装
[root@centos78 ~]# yum install epel-release
[root@centos78 ~]# yum install supervisor

# 2. 使用 easy_install 安装
[root@centos78 ~]# easy_install supervisor
Searching for supervisor
Best match: supervisor 3.4.0
Adding supervisor 3.4.0 to easy-install.pth file
Installing echo_supervisord_conf script to /usr/bin
Installing pidproxy script to /usr/bin
Installing supervisorctl script to /usr/bin
Installing supervisord script to /usr/bin

Using /usr/lib/python2.7/site-packages
Processing dependencies for supervisor
Finished processing dependencies for supervisor

# 3. 生成配置文件
[root@centos78 ~]# mkdir /usr/supervisor
[root@centos78 ~]# echo_supervisord_conf > /usr/supervisor/supervisord.conf

  

1.3 操作命令

[root@centos78 ~]# supervisor
supervisorctl  supervisord

supervisord:   运行Supervisor时会启动一个进程 supervisord,它负责启动所管理的进程,并将所管理的进程作为自己的子进程来启动,而且可以在所管理的进程出现崩溃时自动重启。

supervisorctl: 命令行管理工具,可以用来执行 stop、start、restart 等命令,来对这些子进程进行管理。

supervisor是所有进程的父进程,管理着启动的子进程,supervisor以子进程的PID来管理子进程,当子进程异常退出时supervisor可以收到相应的信号量。

 

1.4 启动supervisord

[root@centos78 ~]# supervisord -c /usr/supervisor/supervisord.conf

 

[root@centos78 ~]# cat /lib/systemd/system/supervisord.service
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf

[Install]
WantedBy=multi-user.target

 

 

二、 进程管理

[root@cl-server ~]# cat /etc/supervisord.d/redis.ini 
[program:redis]
command=/usr/local/bin/redis-server /application/redis/redis.conf         ### redis.conf  的 daemonize 要设置为 no
autostart=true
#autorestart=unexpected
startsecs=10
startretries=3
user=root
#priority=999
redirect_stderr=true                          ### 重定向stderr到 stdout
#stdout_logfile_maxbytes=500MB
#stdout_logfile_backups=10
stdout_logfile=/tmp/redis_6379.log
#stopasgroup=false
#killasgroup=false
stopsignal=KILL                               ### 用来杀死进程的信号
stopwaitsecs=10                               ### 发送SIGKILL前的等待时间

 

2.2 配置详解

;[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到文件描述符的时候,PROCESS_LOG_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
- stopasgroup:如果设置为true,则会使supervisor发送停止信号到整个进程组
- killasgroup:如果设置为true,则在给程序发送SIGKILL信号的时候,会发送到整个进程组,它的子进程也会受到影响。

  

2.3 进程管理

update:            更新新的配置到supervisord(不会重启原来已运行的程序)
reload:            载入所有配置文件,并按新的配置启动、管理所有进程(会重启原来已运行的程序)
start xxx:         启动某个进程
restart xxx:       重启某个进程
stop xxx:          停止某一个进程(xxx),xxx为[program:theprogramname]里配置的值
stop groupworker:  重启所有属于名为groupworker这个分组的进程(start,restart同理)
stop all:          停止全部进程,注:start、restart、stop都不会载入最新的配置文
reread:            当一个服务由自动启动修改为手动启动时执行一下就ok

 

2.4  资源限制

minfds=1024 和 minprocs=200 两个值要做调整, 被管理的进程受到supervisord的主进程影响。

 

三、 配置文件

[root@cl-server ~]# cat /etc/supervisord.conf 
; Sample supervisor config file.

[unix_http_server]
file=/var/run/supervisor/supervisor.sock   ; (the path to the socket file)
;chmod=0700                                ; sockef file mode (default 0700)
;chown=nobody:nogroup                      ; socket file uid:gid owner
;username=user                             ; (default is no username (open server))
;password=123                              ; (default is no password (open server))

;[inet_http_server]                        ; inet (TCP) server disabled by default          开启Web查看进程
;port=127.0.0.1:9001                       ; (ip_address:port specifier, *:port for all iface)
;username=user                             ; (default is no username (open server))
;password=123                              ; (default is no password (open server))

[supervisord]
logfile=/var/log/supervisor/supervisord.log  ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB                        ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10                           ; (num of main logfile rotation backups;default 10)
loglevel=info                                ; (log level;default info; others: debug,warn,trace)
pidfile=/var/run/supervisord.pid             ; (supervisord pidfile;default supervisord.pid)
nodaemon=false               ; (start in foreground if true;default false)
minfds=1024                  ; (min. avail startup file descriptors;default 1024)
minprocs=200                 ; (min. avail process descriptors;default 200)
;umask=022                   ; (process file creation umask;default 022)
;user=chrism                 ; (default is current user, required if root)
;identifier=supervisor       ; (supervisord identifier, default is 'supervisor')
;directory=/tmp              ; (default is not to cd during start)
;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]
;command=/bin/cat              ; 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)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)    
;autostart=true                ; start at supervisord start (default: true)
;autorestart=true              ; retstart at unexpected quit (default: true)
;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_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;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_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           ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

; 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.

;[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  ; each refers to 'x' in [program:x] definitions
;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