Supervisor-守护进程工具
作用
Supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。
问题
不使用守护进程会出现的三个问题:
①ASP.NET Core应用程序运行在shell之中,如果关闭shell则会发现 ASP.NET Core程序被关闭,从而导致应用无法访问,这种情况当然是我们不想遇到的,而且生产环境对这种情况是零容忍的。
②如果 ASP.NET Core进程意外终止那么需要人为连进shell进行再次启动,往往这种操作都不够及时。
③如果服务器宕机或需要重启,我们则还是需要连入shell进行启动。
结果
为了解决这些问题,我们需要有一个程序来监听 ASP.NET Core 应用程序的状况。并在应用程序停止运行的时候立即重新启动。
Supervisor安装与配置 (Centos7)
下载
Code1
2yum install epel-release
yum install -y supervisor配置
下载之后,会在etc目录下有一个
supervisord.conf
的配置文件编辑文件 在文件下加入
Code1
2[include]
files = supervisord.d/*.ini # 守护的进程的配置文件地址如果没有自行创建
supervisord.conf
,内容如下Code1
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; 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
;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 # 守护的进程的配置文件地址配置文件参数说明
Code1
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[unix_http_server]
file=/var/run/supervisor/supervisor.sock ; # socket 文件,supervisorctl 会使用
;chmod=0700 ; # socket文件的mode,默认是0700
;chown=nobody:nogroup ; # socket文件的owner,格式:uid:gid
;[inet_http_server] ; # HTTP服务器,提供web管理界面
;port=127.0.0.1:9001 ; # Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性
;username=user ; # 登录管理后台的用户名
;password=123 ; # 登录管理后台的密码
[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
[supervisorctl]
serverurl=unix:///var/run/supervisor/supervisor.sock ; # 通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致
;serverurl=http://127.0.0.1:9001 ; # 通过HTTP的方式连接supervisord
;[program:theprogramname] # 是被管理的进程配置参数,theprogramname是进程的名称
;command=/bin/cat ; # 程序启动命令
;autostart=true ; # 在supervisord启动的时候也自动启动
;startsecs=10 ; # 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true ; # 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected, ; # 表示进程意外杀死后才重启startretries=3 ; 启动失败自动重试次数,默认是3
user=chrism ; # 用哪个用户启动进程,默认是root
priority=999 ; # 进程启动优先级,默认999,值小的优先启动
redirect_stderr=true ; # 把stderr重定向到stdout,默认false
stdout_logfile_maxbytes=20MB ; # stdout 日志文件大小,默认50MB
stdout_logfile_backups = 20 ; # stdout 日志文件备份数,默认是10
; stdout # 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
stopasgroup=false ; # 默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=false ; # 默认为false,向进程组发送kill信号,包括子进程
;包含其它配置文件
[include]files = relative/directory/.ini ;可以指定一个或多个以.ini结束的配置文件
设置开机自启
Code1
systemctl enable supervisord
启动supervisord服务
Code1
systemctl start supervisord
查看supervisord服务状态
Code1
systemctl status supervisord
supervisord
相关命令更新
Code1
supervisorctl update
重新启动配置中的所有程序
Code1
supervisorctl reload
启动某个进程
Code1
supervisorctl start program_name # program_name=你配置中写的程序名称
查看正在守候的进程
Code1
supervisorctl
停止某一进程
Code1
pervisorctl stop program_name # program_name=你配置中写的程序名称
重启某一进程
Code1
supervisorctl restart program_name # program_name=你配置中写的程序名称停止全部进
停止全部进程
Code1
supervisorctl stop all
显示用stop停止掉的进程,用reload或者update都不会自动重启
获取一些
supervisorctl
其他命令Code1
supervisorctl help
守护Gitbook
服务
我的gitbook
目录是/home/roc/mygitbook
在supervisord.d
目录创建守护我gitbook 的配置
创建demo.ini
内容如下
1 | #项目名 |
更新supervisorctl
1 | supervisorctl update |
它会自动守护你配置的进程
输入supervisorctl
,就可以看到你的进程