Supervisor 简介

Supervisor 是用 Python 开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台 daemon,并监控进程状态,异常退出时能自动重启。它是通过 fork/exec 的方式把这些被管理的进程当作 supervisor 的子进程来启动,这样只要在 supervisor 的配置文件中,把要管理的进程的可执行文件的路径写进去即可。也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。supervisor 还提供了一个功能,可以为 supervisord 或者每个子进程,设置一个非 root 的 user,这个 user 就可以管理它对应的进程。

注:本文以 centos7 为例,supervisor 版本 3.4.0。

Supervisor 安装

Centos 安装

1
yum install supervisor

Debian/Ubuntu 可通过 apt 安装

1
apt-get install supervisor

pip 安装

1
pip install supervisor

easy_install 安装

1
easy_install supervisor

Supervisor 使用

supervisor 配置文件:/etc/supervisord.conf

注:supervisor 的配置文件默认是不全的,不过在大部分默认的情况下,上面说的基本功能已经满足。

子进程配置文件路径:/etc/supervisord.d/

注:默认子进程配置文件为 ini 格式,可在 supervisor 主配置文件中修改。

配置文件说明

Supervisor.conf 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
[unix_http_server]
file=/var/run/supervisor/supervisor.sock ; UNIX socket 文件,supervisorctl 会使用
;chmod=0700 ; socket文件的mode,默认是0700
;chown=nobody:nogroup ; socket文件的owner,格式:uid:gid
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))

;[inet_http_server] ; HTTP服务器,提供web管理界面
;port=0.0.0.0:9001 ; Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性
;username=admin ; 登录管理后台的用户名
;password=s37d32as3 ; 登录管理后台的密码

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; 日志文件,默认是 $CWD/supervisord.log
logfile_maxbytes=50MB ; 日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小
logfile_backups=10 ; 日志文件保留备份数量默认10,设为0表示不备份
loglevel=info ; 日志级别,默认info,其它: debug,warn,trace
pidfile=/var/run/supervisord.pid ; pid 文件
nodaemon=false ; 是否在前台启动,默认是false,即以 daemon 的方式启动
minfds=1024 ; 可以打开的文件描述符的最小值,默认 1024
minprocs=200 ; 可以打开的进程数的最小值,默认 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 ; 通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致
;serverurl=http://127.0.0.1:9001 ; 通过HTTP的方式连接supervisord
;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 ; 程序启动命令,例如:/opt/apache-tomcat-8.0.35/bin/catalina.sh run
;process_name=%(program_name)s ; 在supervisord启动的时候也自动启动
;numprocs=1 ; 启动1秒后没有异常退出,就表示进程正常启动了,默认为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 ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
;autorestart=unexpected ; restart at unexpected quit (default: unexpected)
;startsecs=10 ; number of secs prog must stay running (def. 1)
;startretries=3 ; 启动失败自动重试次数,默认是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 ; 用哪个用户启动进程,默认是root
;redirect_stderr=true ; 把stderr重定向到stdout,默认false
;stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; stdout 日志文件大小,默认50MB
;stdout_logfile_backups=10 ; stdout 日志文件备份数,默认是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 ; 进程启动优先级,默认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 ;可以指定一个或多个以.ini结束的配置文件

子进程配置文件

给需要管理的子进程(程序)编写一个配置文件,放在/etc/supervisor.d/目录下,以.ini 作为扩展名(每个进程的配置文件都可以单独分拆也可以把相关的脚本放一起)。如任意定义一个和脚本相关的项目名称的选项组(/etc/supervisord.d/xxx.ini):

xxx 为 app 名称

此例子为 flask 的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# supervisor的程序名字
[program:xxx]
# supervisor执行的命令
command=uwsgi --ini /opt/pysite/xxx/xxx_uwsgi.ini
# 项目的目录
directory = /opt/pysite/xxx
# 开始的时候等待多少秒
startsecs=10
# 停止的时候等待多少秒
stopwaitsecs=5
# 自动开始
autostart=true
# 程序挂了后自动重启
autorestart=true
# 输出的log文件
stdout_logfile=/opt/logs/xxx/supervisord.log
# 输出的错误文件
stderr_logfile=/opt/logs/xxx/supervisord.err
stopasgroup=true
killasgroup=true

Supervisor 命令说明

常用命令

1
2
3
4
5
6
supervisorctl status        //查看所有进程的状态
supervisorctl stop xxx //停止xxx应用
supervisorctl start xxx //启动xxx应用
supervisorctl restart xxx //重启xxx应用
supervisorctl update //配置文件修改后使用该命令加载新的配置
supervisorctl reload //重新启动配置中的所有程序

注:把 es 换成 all 可以管理配置中的所有进程。直接输入 supervisorctl 进入 supervisorctl 的 shell 交互界面,此时上面的命令不带 supervisorctl 可直接使用。

启动命令

1
2
systemctl start supervisord.service     //启动supervisor并加载默认配置文件
systemctl enable supervisord.service //将supervisor加入开机启动项

注意事项

常见问题

  1. 问题描述:安装好 supervisor 没有开启服务直接使用 supervisorctl 报的错
    1. 问题描述:unix:///var/run/supervisor.sock no such file
    2. 解决办法:supervisord -c /etc/supervisord.conf
  2. command 中指定的进程已经起来,但 supervisor 还不断重启
    1. 问题描述:command 中启动方式为后台启动,导致识别不到 pid,然后不断重启,这里使用的是 elasticsearch,command 指定的是$path/bin/elasticsearch -d
    2. 解决办法:supervisor 无法检测后台启动进程的 pid,而 supervisor 本身就是后台启动守护进程,因此不用担心这个
  3. 启动了多个 supervisord 服务,导致无法正常关闭服务
    1. 问题描述:在运行 supervisord -c /etc/supervisord.conf 之前,直接运行过 supervisord -c /etc/supervisord.d/xx.conf 导致有些进程被多个 superviord 管理,无法正常关闭进程。
    2. 解决办法:使用 ps -fe | grep supervisord 查看所有启动过的 supervisord 服务,kill 相关的进程。