Jusene's Blog

supervisord管理进程

字数统计: 2.4k阅读时长: 13 min
2017/06/21 Share

supervisor

supervisor是由python编写,基于linux操作系统的服务进程管理工具,supervisor是一款c/s框架的系统,可以控制类unix中管理大量进程,supervisor要求管理的程序的是非daemon程序,supervisord会帮它转换成daemon程序。

Supervisor的服务器端称为supervisord,主要负责在启动自身时启动管理的子进程,响应客户端的命令,重启崩溃或退出的子进程,记录子进程stdout和stderr输出,生成和处理子进程生命周期中的事件。可以在一个配置文件中配置相关参数,包括Supervisord自身的状态,其管理的各个子进程的相关属性。配置文件一般位于/etc/supervisord.conf。

Supervisor的客户端称为supervisorctl,它提供了一个类shell的接口(即命令行)来使用supervisord服务端提供的功能。通过supervisorctl,用户可以连接到supervisord服务器进程,获得服务器进程控制的子进程的状态,启动和停止子进程,获得正在运行的进程列表。客户端通过Unix域套接字或者TCP套接字与服务端进行通信,服务器端具有身份凭证认证机制,可以有效提升安全性。当客户端和服务器位于同一台机器上时,客户端与服务器共用同一个配置文件/etc/supervisord.conf,通过不同标签来区分两者的配置。

upervisor也提供了一个web页面来查看和管理进程状态。

安装supervisor

下载:https://pypi.python.org/packages/7b/17/88adf8cb25f80e2bc0d18e094fcd7ab300632ea00b601cbbbb84c2419eae/supervisor-3.3.2.tar.gz

中间的小插曲:

1
~]# tar xf supervisor-3.3.2.tar.gz
2
~]# cd supervisor-3.3.2
3
~]# python setup.py install
4
5
报ssl证书认证失败
6
解决办法:
7
~]# wget http://curl.haxx.se/ca/cacert.pem
8
~]# mv cacert.pem ca-bundle.crt
9
~]# mv ca-bundle.crt /etc/pki/tls/certs 
10
11
还是报错,检查下时间,时间修正,supervisor安装完成。

supervisor配置

生成supervisor的模版配置文件:

1
~]# echo_supervisord_conf > /etc/supervisord.conf
1
~]# cat /etc/supervisord.conf
2
; Sample supervisor config file.
3
;
4
; For more information on the config file, please see:
5
; http://supervisord.org/configuration.html
6
;
7
; Notes:
8
;  - Shell expansion ("~" or "$HOME") is not supported.  Environment
9
;    variables can be expanded using this syntax: "%(ENV_HOME)s".
10
;  - Quotes around values are not supported, except in the case of
11
;    the environment= options as shown below.
12
;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
13
;  - Command will be truncated if it looks like a config file comment, e.g.
14
;    "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".
15
16
[unix_http_server]          #supervisord socket管理接口
17
file=/tmp/supervisor.sock   ; the path to the socket file
18
;chmod=0700                 ; socket file mode (default 0700)
19
;chown=nobody:nogroup       ; socket file uid:gid owner
20
;username=user              ; default is no username (open server)
21
;password=123               ; default is no password (open server)
22
23
;[inet_http_server]         ; inet (TCP) server disabled by default  #web管理接口
24
;port=127.0.0.1:9001        ; ip_address:port specifier, *:port for all iface
25
;username=user              ; default is no username (open server)
26
;password=123               ; default is no password (open server)
27
28
[supervisord]                #supervisord服务端配置
29
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
30
logfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MB
31
logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
32
loglevel=info                ; log level; default info; others: debug,warn,trace
33
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
34
nodaemon=false               ; start in foreground if true; default false
35
minfds=1024                  ; min. avail startup file descriptors; default 1024
36
minprocs=200                 ; min. avail process descriptors;default 200
37
;umask=022                   ; process file creation umask; default 022
38
;user=chrism                 ; default is current user, required if root
39
;identifier=supervisor       ; supervisord identifier, default is 'supervisor'
40
;directory=/tmp              ; default is not to cd during start
41
;nocleanup=true              ; don't clean up tempfiles at start; default false
42
;childlogdir=/tmp            ; 'AUTO' child log dir, default $TEMP
43
;environment=KEY="value"     ; key value pairs to add to environment
44
;strip_ansi=false            ; strip ansi escape codes in logs; def. false
45
46
; The rpcinterface:supervisor section must remain in the config file for
47
; RPC (supervisorctl/web interface) to work.  Additional interfaces may be
48
; added by defining them in separate [rpcinterface:x] sections.
49
50
[rpcinterface:supervisor]
51
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
52
53
; The supervisorctl section configures how supervisorctl will connect to
54
; supervisord.  configure it match the settings in either the unix_http_server
55
; or inet_http_server section.
56
57
[supervisorctl]              #supervisor的管理配置
58
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
59
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
60
;username=chris              ; should be same as in [*_http_server] if set
61
;password=123                ; should be same as in [*_http_server] if set
62
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
63
;history_file=~/.sc_history  ; use readline history if available
64
65
; The sample program section below shows all possible program subsection values.
66
; Create one or more 'real' program: sections to be able to control them under
67
; supervisor.
68
69
;[program:theprogramname]      #管理的单个进程配置,可以添加多个
70
;command=/bin/cat              ; the program (relative uses PATH, can take args)
71
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
72
;numprocs=1                    ; number of processes copies to start (def 1)
73
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
74
;umask=022                     ; umask for process (default None)
75
;priority=999                  ; the relative start priority (default 999)
76
;autostart=true                ; start at supervisord start (default: true)
77
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
78
;startretries=3                ; max # of serial start failures when starting (default 3)
79
;autorestart=unexpected        ; when to restart if exited after running (def: unexpected)
80
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
81
;stopsignal=QUIT               ; signal used to kill process (default TERM)
82
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
83
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
84
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
85
;user=chrism                   ; setuid to this UNIX account to run the program
86
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
87
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
88
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
89
;stdout_logfile_backups=10     ; # of stdout logfile backups (0 means none, default 10)
90
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
91
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
92
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
93
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
94
;stderr_logfile_backups=10     ; # of stderr logfile backups (0 means none, default 10)
95
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
96
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
97
;environment=A="1",B="2"       ; process environment additions (def no adds)
98
;serverurl=AUTO                ; override serverurl computation (childutils)
99
100
; The sample eventlistener section below shows all possible eventlistener
101
; subsection values.  Create one or more 'real' eventlistener: sections to be
102
; able to handle event notifications sent by supervisord.
103
104
;[eventlistener:theeventlistenername]
105
;command=/bin/eventlistener    ; the program (relative uses PATH, can take args)
106
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
107
;numprocs=1                    ; number of processes copies to start (def 1)
108
;events=EVENT                  ; event notif. types to subscribe to (req'd)
109
;buffer_size=10                ; event buffer queue size (default 10)
110
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
111
;umask=022                     ; umask for process (default None)
112
;priority=-1                   ; the relative start priority (default -1)
113
;autostart=true                ; start at supervisord start (default: true)
114
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
115
;startretries=3                ; max # of serial start failures when starting (default 3)
116
;autorestart=unexpected        ; autorestart if exited after running (def: unexpected)
117
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
118
;stopsignal=QUIT               ; signal used to kill process (default TERM)
119
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
120
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
121
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
122
;user=chrism                   ; setuid to this UNIX account to run the program
123
;redirect_stderr=false         ; redirect_stderr=true is not allowed for eventlisteners
124
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
125
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
126
;stdout_logfile_backups=10     ; # of stdout logfile backups (0 means none, default 10)
127
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
128
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
129
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
130
;stderr_logfile_backups=10     ; # of stderr logfile backups (0 means none, default 10)
131
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
132
;environment=A="1",B="2"       ; process environment additions
133
;serverurl=AUTO                ; override serverurl computation (childutils)
134
135
; The sample group section below shows all possible group values.  Create one
136
; or more 'real' group: sections to create "heterogeneous" process groups.
137
138
;[group:thegroupname]          #配置一组进程,对于类似的progname可以通过这种方式添加,避免一个个添加
139
;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions
140
;priority=999                  ; the relative start priority (default 999)
141
142
; The [include] section can just contain the "files" setting.  This
143
; setting can list multiple files (separated by whitespace or
144
; newlines).  It can also contain wildcards.  The filenames are
145
; interpreted as relative to this file.  Included files *cannot*
146
; include files themselves.
147
148
;[include]
149
;files = relative/directory/*.ini

写个小网页:

1
~]# cat www.py
2
from flask import Flask
3
app=Flask(__name__)
4
5
@app.route('/')
6
def hello():
7
	return 'hello world'
8
9
if __name__ == '__main__':
10
	app.run(host='0.0.0.0')

配置一个program:

1
[program:flask]
2
command=python /root/www.py
3
autostart=true
4
autorestart=true
5
startsecs=1
6
stdout_logfile=/var/log/flask.log
7
stderr_logfile=/var/log/flask.error

启动:

1
~]# supervisord -c /etc/supervisord.conf
2
~]# ps aux | grep sup
3
root      1414  0.0  0.5 215032 10092 ?        Ss   11:21   0:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
4
root      1433  0.0  0.0 112648   964 pts/0    S+   11:21   0:00 grep --color=auto sup
5
~]# ~]# curl 127.0.0.1:5000
6
hello world

supervisord管理

Supervisord安装完成后有两个可用的命令行supervisord和supervisorctl,
命令使用解释如下:

  • supervisord, 初始启动Supervisord,启动、管理配置中设置的进程。
  • supervisorctl stop programxxx, 停止某一个进程(programxxx),programxxx为[program:chatdemon]里配置的值,这个示例就是chatdemon。
  • supervisorctl start programxxx, 启动某个进程
  • supervisorctl restart programxxx,重启某个进程
  • supervisorctl stop groupworker: ,重启所有属于名为groupworker这个分组的进程(start,restart同理)
  • supervisorctl stop all, 停止全部进程,注:start、restart、stop都不会载入最新的配置文件。
  • supervisorctl reload, 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程。
  • supervisorctl update, 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。

注意:显示用stop停止掉的进程,用reload或者update都不会自动重启。

web页面管理

1
[inet_http_server]         ; inet (TCP) server disabled by default
2
port=0.0.0.0:9001          ; ip_address:port specifier, *:port for all iface
3
username=user              ; default is no username (open server)
4
password=123               ; default is no password (open server)

CATALOG
  1. 1. supervisor
  2. 2. 安装supervisor
  3. 3. supervisor配置
  4. 4. supervisord管理
  5. 5. web页面管理