参考文献

官方文档

环境说明

  • Supervisor
  • CentOS Linux release 8.3.2011

步骤说明

安装

yum安装

sudo su - #切换为root用户
yum install -y epel-release
yum install -y supervisor

配置

生成配置
echo_supervisord_conf > /etc/supervisord.conf

设置开机启动
systemctl enable supervisord

开启服务
systemctl start supervisord

查看supervisord服务状态
systemctl status supervisord

[root@iZbp14vt99zpj8dde2fdlmZ ~]# systemctl status supervisord
● supervisord.service - Process Monitoring and Control Daemon
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-04-29 16:07:29 CST; 6min ago
  Process: 53851 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)
 Main PID: 53885 (supervisord)
    Tasks: 1 (limit: 47779)
   Memory: 16.3M
   CGroup: /system.slice/supervisord.service
           └─53885 /usr/bin/python3.6 /usr/bin/supervisord -c /etc/supervisord.conf

Apr 29 16:07:29 iZbp14vt99zpj8dde2fdlmZ systemd[1]: Starting Process Monitoring and Control Daemon...
Apr 29 16:07:29 iZbp14vt99zpj8dde2fdlmZ systemd[1]: Started Process Monitoring and Control Daemon.

修改配置

vim /etc/supervisord.conf

;[inet_http_server]         ; inet (TCP) server disabled by default
;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)
改
[inet_http_server]         ; inet (TCP) server disabled by default
port=*:端口号        ; ip_address:port specifier, *:port for all iface
username=账号              ; default is no username (open server)
password=密码  ; default is no password (open server)

注:以上 端口 与 账号密码 按需配置

;[include]
;files = relative/directory/*.ini
改
[include]
files = supervisord.d/*.ini

注:目录按需配置

修改完配置后重启
systemctl restart supervisord.service

输入你的服务地址,或你本地测试地址加配置中的端口号

注:如果是阿里云,需要配置安全组端口开放

登录进去后,效果如下

监听配置

我这里贴一下我的监听配置文件(仅供参考)

# 程序名称
[program:TaskExpire]
# 执行命令
command = php think task_expire
# 启动个数
numprocs = 1
# 程序路径
directory = /home/wwwroot/www.axinblog.com/
# 自动开始
autostart = true
# 自动重启
autorestart = true
# 进程启动后运行秒钟,才被认定为成功启动
startsecs = 10
# 启动失败自动重试次数
startretries = 3
# 进程退出码
exitcodes = 0
# 进程停止信号
stopsignal = KILL
# 向子进程发送stopsignal信号后,到系统返回信息给supervisord,所等待的最大时间
stopwaitsecs = 10
# 日志输出
stderr_logfile = /home/wwwroot/www.axinblog.com/log/task_expire_stderr.log
stdout_logfile = /home/wwwroot/www.axinblog.com/log/task_expire_stdout.log
# 把stderr重定向到stdout,默认 false
redirect_stderr = true
# stdout日志文件大小,默认 50MB
stdout_logfile_maxbytes = 20MB
# stdout日志文件备份数
stdout_logfile_backups = 20

把命令配置文件保存到上方supervisord.conf配置中的include目录下

例:我的监听配置目录为

[include]
files = supervisord.d/*.ini

重启服务
supervisorctl reloadsystemctl restart supervisord.service

效果如下

监听配置详解

; The sample eventlistener section below shows all possible eventlistener
; subsection values.  Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.

;[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)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; autorestart if exited after running (def: unexpected)
;exitcodes=0                   ; 'expected' exit codes used with autorestart (default 0)
;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 (0 means none, default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stdout_syslog=false           ; send stdout to syslog with process name (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 (0 means none, default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;stderr_syslog=false           ; send stderr to syslog with process name (default false)
;environment=A="1",B="2"       ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

命令扩展

supervisor 、supervisord、supervisorctl

supervisor:要安装的软件的名称。
supervisord:supervisor的守护进程(一般结尾是d的都是守护进程)装好supervisor后,supervisord用于启动supervisor服务。
supervisorctl:用于管理supervisor配置文件中的各种其他进程。

输出版本

supervisord -v
4.2.1

重启
supervisorctl reload

[root@iZbp14vt99zpj8dde2fdlmZ ~]# supervisorctl reload
Restarted supervisord

查看所有监听状态
supervisorctl status

[root@iZbp14vt99zpj8dde2fdlmZ ~]# supervisorctl status
ReviewExpire                     RUNNING   pid 89060, uptime 18:49:53
SubmitExpire                     RUNNING   pid 89061, uptime 18:49:53
TaskExpire                       RUNNING   pid 89062, uptime 18:49:53

停止监听
supervisorctl stop <监听配置中program的名称>

[root@iZbp14vt99zpj8dde2fdlmZ ~]# supervisorctl stop
Error: stop requires a process name
stop <name>             Stop a process
stop <gname>:*          Stop all processes in a group
stop <name> <name>      Stop multiple processes or groups
stop all                Stop all processes

启动监听
supervisorctl start <监听配置中program的名称>

[root@iZbp14vt99zpj8dde2fdlmZ ~]# supervisorctl start
Error: start requires a process name
start <name>            Start a process
start <gname>:*         Start all processes in a group
start <name> <name>     Start multiple processes or groups
start all               Start all processes

监听配置文件修改后使用该命令加载新的配置
supervisorctl update
示例:

[root@iZbp14vt99zpj8dde2fdlmZ supervisord.d]# supervisorctl update
DtRankSettle: added process group
DtRobotJoin: added process group

查看supervisord进程
ps -ef | grep supervisord