Supervisor的使用

supervisor

Supervisord 是用 Python 实现的一款非常实用的进程管理工具,supervisord 还要求管理的程序是非 daemon 程序,supervisord 会帮你把它转成 daemon 程序,因此如果用 supervisord 来管理 nginx 的话,必须在 nginx 的配置文件里添加一行设置 daemon off 让 nginx 以非 daemon 方式启动。

1. 安装 与 默认配置

1
2
3
4
5
apt-cache search python-setuptools
sudo apt-get install python-setuptools
easy_install supervisor
supervisord -v
echo_supervisord_conf > /etc/supervisord.conf # 默认的配置 输入 /etc/supvervisord.conf

修改为自己习惯的配置 /etc/supvervisord.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
[unix_http_server]          ; supervisord的unix socket服务配置
file=/usr/local/deploy/supervisord/supervisor.sock ; socket文件的保存目录

[inet_http_server] ; supervisord的tcp服务配置
port=*:9001 ; tcp端口

[supervisord] ; supervisord的主进程配置
logfile=/usr/local/deploy/supervisord/supervisord.log ; 主要的进程日志配置
logfile_maxbytes=50MB ; 最大日志体积,默认50MB
logfile_backups=10 ; 日志文件备份数目,默认10
loglevel=info ; 日志级别,默认info; 还有:debug,warn,trace
pidfile=/usr/local/deploy/supervisord/supervisord.pid ; supervisord的pidfile文件
nodaemon=false ; 是否以守护进程的方式启动
minfds=1024 ; 最小的有效文件描述符,默认1024
minprocs=200 ; 最小的有效进程描述符,默认200

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

[supervisorctl]
serverurl=unix:///usr/local/deploy/supervisord/supervisor.sock ; use a unix:// URL for a unix socket

[include]
files = /usr/local/deploy/supervisord/conf/*.conf

2. 加载自定义配置目录 vim /etc/supervisord.conf

1
2
3
4
5
mkdir -p /usr/local/deploy/supervisord/conf/

;...
[include]
files = /usr/local/deploy/supervisord/conf/*.conf

3. 新建管理的应用

1
2
cd /usr/local/deploy/supervisord/conf/
vim express-service.conf

内容:

1
2
3
4
5
6
[program:express-service]
command=node /home/vagrant/test-service/app.js
autostart=false
autorestart=false
stdout_logfile=/usr/local/deploy/log/express-service.out
stderr_logfile=/usr/local/deploy/log/express-service.err

如果报错,可以使用 supervisorctl tail express-service stdout 来查看具体报错。

另外配置文件可使用:%(directory) 的格式来引用变量。

常见问题:

  1. 直接访问http://127.0.0.1:9001/ 可以查看 Tail -f的日志,通过Nginx代理后则不行;Nginx需要增加以下配置,参考Supervisord inet_http_server behind nginx
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    server {
    listen 9000;
    listen [::]:9000;

    server_name server.gmetri.com;

    location / {
    proxy_pass http://127.0.0.1:9001;
    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_max_temp_file_size 0;
    proxy_redirect default;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Connection "";
    }
    }

supervisord 管理

启动:supervisord (默认会读/etc/supervisord.conf)或者 指定主配置supervisord -c /etc/supervisord.conf

  • supervisorctl 不跟其他,进入 supervisorctl console, status 可以查看所有任务的状态,uptime等,新增任务需要 update
  • supervisorctl update,根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。
  • supervisorctl reload,载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程。
  • supervisorctl stop xxx 停止某一个进程

附 task 配置 Demo

auth-service.conf:

1
2
3
4
5
6
[program:auth-service]
command=java -D"app.id=gateway" -D"env=FAT" -javaagent:/usr/local/skywalking-agent/skywalking-agent.jar -jar /usr/local/deploy/auth-service-exec.jar
autostart=false
autorestart=false
stdout_logfile=/usr/local/deploy/log/auth-service.out
stderr_logfile=/usr/local/deploy/log/auth-service.err

shadowsocks.conf:

1
2
3
4
5
[program:shadowsocks]
command=sslocal -c ~/shadowsocks.json
autostart=true
autorestart=true
user=nobody

附 program 配置

http://www.supervisord.org/configuration.html#program-x-section-example

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
[program:cat]
command=/bin/cat
process_name=%(program_name)s
numprocs=1
directory=/tmp
umask=022
priority=999
autostart=true
autorestart=unexpected
startsecs=10
startretries=3
exitcodes=0,2
stopsignal=TERM
stopwaitsecs=10
stopasgroup=false
killasgroup=false
user=chrism
redirect_stderr=false
stdout_logfile=/a/path
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stdout_events_enabled=false
stderr_logfile=/a/path
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB
stderr_events_enabled=false
environment=A="1",B="2"
serverurl=AUTO

附 supervisor 配置说明:

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Note: shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".

[unix_http_server] ; supervisord的unix socket服务配置
file=/tmp/supervisor.sock ; socket文件的保存目录
;chmod=0700 ; socket的文件权限 (default 0700)
;chown=nobody:nogroup ; socket的拥有者和组名
;username=user ; 默认不需要登陆用户 (open server)
;password=123 ; 默认不需要登陆密码 (open server)

;[inet_http_server] ; supervisord的tcp服务配置
;port=127.0.0.1:9001 ; tcp端口
;username=user ; tcp登陆用户
;password=123 ; tcp登陆密码

[supervisord] ; supervisord的主进程配置
logfile=/tmp/supervisord.log ; 主要的进程日志配置
logfile_maxbytes=50MB ; 最大日志体积,默认50MB
logfile_backups=10 ; 日志文件备份数目,默认10
loglevel=info ; 日志级别,默认info; 还有:debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord的pidfile文件
nodaemon=false ; 是否以守护进程的方式启动
minfds=1024 ; 最小的有效文件描述符,默认1024
minprocs=200 ; 最小的有效进程描述符,默认200
;umask=022 ; 进程文件的umask,默认200
;user=chrism ; 默认为当前用户,如果为root则必填
;identifier=supervisor ; supervisord的表示符, 默认时'supervisor'
;directory=/tmp ; 默认不cd到当前目录
;nocleanup=true ; 不在启动的时候清除临时文件,默认false
;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value ; 初始键值对传递给进程
;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:///tmp/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 ; 如果设置应该与http_username相同
;password=123 ; 如果设置应该与http_password相同
;prompt=mysupervisor ; 命令行提示符,默认"supervisor"
;history_file=~/.sc_history ; 命令行历史纪录

; 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 ; 运行的程序 (相对使用PATH路径, 可以使用参数)
;process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s
;numprocs=1 ; 默认启动的进程数目,默认为1
;directory=/tmp ; 在运行前cwd到指定的目录,默认不执行cmd
;umask=022 ; 进程umask,默认None
;priority=999 ; 程序运行的优先级,默认999
;autostart=true ; 默认随supervisord自动启动,默认true
;autorestart=unexpected ; whether/when to restart (default: unexpected)
;startsecs=1 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 期望的退出码,默认0,2
;stopsignal=QUIT ; 杀死进程的信号,默认TERM
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; 向unix进程组发送停止信号,默认false
;killasgroup=false ; 向unix进程组发送SIGKILL信号,默认false
;user=chrism ; 为运行程序的unix帐号设置setuid
;redirect_stderr=true ; 将标准错误重定向到标准输出,默认false
;stdout_logfile=/a/path ; 标准输出的文件路径NONE=none;默认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 ; 运行的程序 (相对使用PATH路径, 可以使用参数)
;process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s
;numprocs=1 ; 默认启动的进程数目,默认为1
;events=EVENT ; event notif. types to subscribe to (req'd)
;buffer_size=10 ; 事件缓冲区队列大小,默认10
;directory=/tmp ; 在运行前cwd到指定的目录,默认不执行cmd
;umask=022 ; 进程umask,默认None
;priority=-1 ; 程序运行的优先级,默认-1
;autostart=true ; 默认随supervisord自动启动,默认true
;autorestart=unexpected ; whether/when to restart (default: unexpected)
;startsecs=1 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 期望的退出码,默认0,2
;stopsignal=QUIT ; 杀死进程的信号,默认TERM
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; 向unix进程组发送停止信号,默认false
;killasgroup=false ; 向unix进程组发送SIGKILL信号,默认false
;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 ; 任何在[program:x]中定义的x
;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 = relative/directory/*.ini

作者:BlackRun
链接:https://www.jianshu.com/p/5c3922fa97c0
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。