CentOS7 supervisor 安装与配置

安装

1
yum install supervisor

配置文件目录

1
2
3
4
5
/etc/
	supervisor.conf
	supervisor.d/
		项目一.ini
		项目二.ini

laravel 队列配置示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
说明, user=www 为启动的 php artisan 进程执行用户,这个要和 php 执行用户使用一个,而 supervisor 启动用户为 root, 配置在 supervisor.conf 文件中,stdout 等日志文件创建者均为 supervisor 用户,即 root

supervisor 主配置文件路径为 /var/log/supervisor/supervisord.log (版本间可能不同,请查看主配置文件 /etc/supervisor.conf logfile 配置项),写 stdout 等日志路径时,可使用 /var/log/supervisor/{programname 即 smallnews}.log

[program:smallnews]
process_name=%(program_name)s_%(process_num)02d
command=/usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon
numprocs=3
autostart=true
autorestart=true
user=www
redirect_stderr=true
stdout_logfile=/var/log/supervisor/smallnews.log
stderr_logfile=/var/log/supervisor/smallnews_err.log

常用命令

1
2
3
4
5
6
7
8
启动与停止
stop 之后相关的进程全部被杀死,restart 相关进程全部重启
开机自启动
systemctl enable supervisord
启动停止重启
systemctl start|stop|restart supervisord
状态
systemctl status supervisord

状态信息

 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
正在运行
● supervisord.service - Process Monitoring and Control Daemon
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-05-25 17:53:33 CST; 1min 34s ago
  Process: 20182 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)
 Main PID: 20185 (supervisord)
   CGroup: /system.slice/supervisord.service
           ├─20185 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
           ├─20186 /usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon
           ├─20187 /usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon
           └─20188 /usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon
停止运行
● supervisord.service - Process Monitoring and Control Daemon
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
查看启动的进程
主进程
#> ps -ef | grep supervisor
root     23668     1  0 18:29 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf

子进程 即 supervisor 坚守的进程
#> ps -ef | grep artisan
www      23669 23668  0 18:29 ?        00:00:00 /usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon
www      23670 23668  0 18:29 ?        00:00:00 /usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon
www      23671 23668  0 18:29 ?        00:00:00 /usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon

网页查看状态

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
[inet_http_server]         ; inet (TCP) server disabled by default
port=0.0.0.0: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))
访问网址查看 Supervisor status,可直接操作启动,重启,停止等指令

ip:9001 
账号:user
密码:123
State	Description	Name	Action
running	pid 22506, uptime 0:00:28	smallnews:smallnews_00	Restart Stop Clear Log Tail -f
running	pid 22505, uptime 0:00:28	smallnews:smallnews_01	Restart Stop Clear Log Tail -f
running	pid 22504, uptime 0:00:28	smallnews:smallnews_02	Restart Stop Clear Log Tail -f

配置项说明

 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
摘取至上面文档

;*为必须填写项
;*[program:应用名称]
[program:cat]
;*命令路径,如果使用python启动的程序应该为 python /home/test.py, 
;不建议放入/home/user/, 对于非user用户一般情况下是不能访问
command=/bin/cat
;当numprocs为1时,process_name=%(program_name)s
;当numprocs>=2时,%(program_name)s_%(process_num)02d
process_name=%(program_name)s
;进程数量
numprocs=1
;执行目录,若有/home/supervisor_test/test1.py
;将directory设置成/home/supervisor_test
;则command只需设置成python test1.py
;否则command必须设置成绝对执行目录
directory=/tmp
;掩码:--- -w- -w-, 转换后rwx r-x w-x
umask=022
;优先级,值越高,最后启动,最先被关闭,默认值999
priority=999
;如果是true,当supervisor启动时,程序将会自动启动
autostart=true
;*自动重启
autorestart=true
;启动延时执行,默认1秒
startsecs=10
;启动尝试次数,默认3次
startretries=3
;当退出码是0,2时,执行重启,默认值0,2
exitcodes=0,2
;停止信号,默认TERM
;中断:INT(类似于Ctrl+C)(kill -INT pid),退出后会将写文件或日志(推荐)
;终止:TERM(kill -TERM pid)
;挂起:HUP(kill -HUP pid),注意与Ctrl+Z/kill -stop pid不同
;从容停止:QUIT(kill -QUIT pid)
;KILL, USR1, USR2其他见命令(kill -l),说明1
stopsignal=TERM
stopwaitsecs=10
;*以root用户执行,如果是 php 相关进程,请使用 php 启动用户
user=root
;重定向
redirect_stderr=false
stdout_logfile=/a/path
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stderr_logfile=/a/path
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB
;环境变量设置
environment=A="1",B="2"
serverurl=AUTO

常见问题

问题一 启动报错

 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
描述
#> systemctl restart supervisord

Job for supervisord.service failed because the control process exited with error code. See "systemctl status supervisord.service" and "journalctl -xe" for details

#> systemctl status supervisord.service

supervisord.service - Process Monitoring and Control Daemon
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Sat 2019-05-25 18:23:46 CST; 13s ago
  Process: 23126 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=2)
 Main PID: 22502 (code=exited, status=0/SUCCESS)

May 25 18:23:46 VM_115_62_centos systemd[1]: Starting Process Monitoring and Control Daemon...
May 25 18:23:46 VM_115_62_centos supervisord[23126]: Error: section [inet_http_server] has no port value
May 25 18:23:46 VM_115_62_centos supervisord[23126]: For help, use /usr/bin/supervisord -h
May 25 18:23:46 VM_115_62_centos systemd[1]: supervisord.service: control process exited, code=exited status=2
May 25 18:23:46 VM_115_62_centos systemd[1]: Failed to start Process Monitoring and Control Daemon.
May 25 18:23:46 VM_115_62_centos systemd[1]: Unit supervisord.service entered failed state.
May 25 18:23:46 VM_115_62_centos systemd[1]: supervisord.service failed.
从上面得出报错信息为

Error: section [inet_http_server] has no port value
解决
如果存在 [inet_http_server] 则必须存在 127.0.0.1::9001 或者 0.0.0.0:9001,即必须启动这个监听进程
如果想关掉 必须把 [inet_http_server] 注释掉,即最前面加 “;”

问题二 启动报错

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
描述
#> systemctl restart supervisord

Job for supervisord.service failed because the control process exited with error code. See "systemctl status supervisord.service" and "journalctl -xe" for details

#> systemctl status supervisord.service

● 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 Sat 2021-03-27 18:28:47 CST; 5min ago
  Process: 19173 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=2)

Mar 27 18:28:47 VM-0-14-centos systemd[1]: Starting Process Monitoring and Control Daemon...
Mar 27 18:28:47 VM-0-14-centos supervisord[19173]: Error: The directory named as part of the path /data/log/supervisor/laraveldy_timing.log does...ng.ini')
Mar 27 18:28:47 VM-0-14-centos supervisord[19173]: For help, use /usr/bin/supervisord -h
Mar 27 18:28:47 VM-0-14-centos systemd[1]: supervisord.service: control process exited, code=exited status=2
Mar 27 18:28:47 VM-0-14-centos systemd[1]: Failed to start Process Monitoring and Control Daemon.
Mar 27 18:28:47 VM-0-14-centos systemd[1]: Unit supervisord.service entered failed state.
Mar 27 18:28:47 VM-0-14-centos systemd[1]: supervisord.service failed.
从上面得出报错信息为
Error: /data/log/supervisor/laraveldy_timing.log 文件不存在
解决
手动创建文件