supervisor 服务监控

supervisor 服务进程监控

1
yum install -y supervisor

配置文件介绍

配置文件: /etc/supervisord.conf

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

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
[unix_http_server]
file=/var/run/supervisor/supervisor.sock ; UNIX socket 文件,supervisorctl 会使用
;chmod=0700 ; socket文件的权限 (default 0700)
;chown=nobody:nogroup ; socket文件的组 uid:gid
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))

;[inet_http_server] ; Http服务器,提供WEB管理界面
;port=127.0.0.1:9001 ; 管理的IP和端口,注意安全性
;username=user ; 管理后台的用户名
;password=123 ; 管理后台的密码

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; 日志文件
logfile_maxbytes=50MB ; 日志文件的大小,默认为 50MB,设置为0则不限制大小
logfile_backups=10 ; 日志文件保留备份数量,默认为 10,设置为0则不备份
loglevel=info ; 日志级别
pidfile=/var/run/supervisord.pid ; pid文件
nodaemon=false ; 是否在前台启动,false -> 以 daemon 方式启动
minfds=1024 ; 可以打开的文件描述符的最小值
minprocs=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链接
;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 ; 程序启动的命令
;process_name=%(program_name)s ; 进程名
;numprocs=1 ; 需要启动的进程数量
;directory=/tmp ; 脚本目录
;umask=022 ; umask for process (default None)
;priority=999 ; 启动优先级 默认为999 值小的优先启动
;autostart=true ; 在 supervisord 启动的时候启动 (default: true)
;autorestart=true ; 程序退出自动重启 (default: true)
;startsecs=10 ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒 (def. 1)
;startretries=3 ; 启动失败自动重试次数,默认是3 (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 ; 用哪个用户启动
;redirect_stderr=true ; 将stderr重定向到stdout (default false)
;stdout_logfile=/a/path ; 日志输出, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; 日志文件大小 (default 50MB)
;stdout_logfile_backups=10 ; 日志的备份数量 (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 ; 环境变量 (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)

; 对子进程的监控,当进程异常退出时告警

;[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)

; 将多个文件组合成一个组

;[group:thegroupname]
;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
;priority=999 ; the relative start priority (default 999)

; 包含其他文件

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

实例

管理 Gogs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
directory=/home/git/go/src/github.com/gogits/gogs/
command=/home/git/go/src/github.com/gogits/gogs/gogs web
autostart=true
autorestart=true
startsecs=10
stdout_logfile=/var/log/gogs/stdout.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stderr_logfile=/var/log/gogs/stderr.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB
user = git
environment = HOME="/home/git", USER="git"

管理MySQL

1
2
3
4
5
6
7
8
[program:mysql]
command=/opt/mysql/bin/mysqld_safe --defaults-file=/opt/mysql/conf/my.ini
user=ifan
autostart=true
autorestart=true
directory=/home/ifan/software/mysql
stderr_logfile=/var/mysql/logs/err.log
stdout_logfile=/var/mysql/logs/out.log

管理redis

注意需要关闭daemonize no后台守护进程

1
2
3
4
5
6
7
8
[program:redis]
command=/opt/redis/bin/redis-server /opt/redis/conf/redis.conf
user=ifan
autostart=true
autorestart=true
directory=/opt/redis/
stderr_logfile=/var/redis/log/err.log
stdout_logfile=/var/redis/log/out.log

CMD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 设置为开机自启
systemctl enable supervisord
# 启动
systemctl start supervisord.service
# 查看状态
systemctl status supervisord.service
# 停止
systemctl stop supervisord.service
# 重启
systemctl restart supervisord.service

# 重新加载配置文件
supervisorctl reload
# 查看进程
supervisorctl status
# 启动某个进程
supervisorctl start xxxx
# 停止某个进程
supervisorctl stop xxxx
# 重启某个进程
supervisorctl restart xxxx

报错

无法重启

stackoverflow

1
2
3
4
5
6
7
8
9
10
11
12
13
14

● supervisord.service - Process Monitoring and Control Daemon
Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Thu 2021-04-15 22:49:51 CST; 6s ago
Process: 27052 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=2)

Apr 15 22:49:50 iFan systemd[1]: Starting Process Monitoring and Control Daemon...
Apr 15 22:49:51 iFan supervisord[27052]: Error: Another program is already listening on a port that one of our HTT...isord.
Apr 15 22:49:51 iFan supervisord[27052]: For help, use /usr/bin/supervisord -h
Apr 15 22:49:51 iFan systemd[1]: supervisord.service: control process exited, code=exited status=2
Apr 15 22:49:51 iFan systemd[1]: Failed to start Process Monitoring and Control Daemon.
Apr 15 22:49:51 iFan systemd[1]: Unit supervisord.service entered failed state.
Apr 15 22:49:51 iFan systemd[1]: supervisord.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
1
unlink /var/run/supervisor/supervisor.sock