Contents

Supervisor的使用

简介

  • Supervisor 是一个进程管理工具。用于管理和监控进程的客户端/服务器系统,它可用于 Linux 和 Unix 系统。Supervisor 提供了一个守护进程,可以监控指定的进程,并在进程异常退出时自动重新启动它们。

安装supervisor的时候,遇到了一些问题,比如:

  • unix:///var/run/supervisor.sock no such file
  • unix:///var/run/supervisor.sock refused connection
  • pkg_resources.VersionConflict: (supervisor 4.0.4 (/usr/local/lib/python3.7/dist-packages), Requirement.parse(‘supervisor==3.3.1’))

解决:

  • 不用apt-get直接安装,可能是源没有supervisor 4.0.4版本

环境:

1
2
3
4
5
6
7
8
9
 % lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.2 LTS
Release:        18.04
Codename:       bionic

 % python -V
Python 3.6.8

步骤,注意版本号

  • 安装python包
1
sudo python3.6 -m pip install supervisor==4.0.4
  • 从git下载supervisor,解压并cd进去
1
2
3
4
wget -O supervisor-4.0.4.zip https://github.com/Supervisor/supervisor/tree/4.0.4
unzip supervisor-4.0.4.zip 
cd supervisor-4.0.4
sudo python -m setup.py install
  • 记一下配置
 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
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)
; 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.sock ; use a unix:// URL  for a unix socket

; 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 = /etc/supervisor/conf.d/*.conf
  • 根据配置生成日志文件夹
1
2
sudo mkdir -p /var/log/supervisor/
sudo touch /var/log/supervisor/supervisord.log
  • 好了
1
2
sudo supervisord -c /etc/supervisord.conf
sudo supervisorctl restart

配置队列

 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
sudo vim /etc/supervisor/conf.d/laravel-worker.conf

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php artisan queue:work --sleep=3 --tries=3 --queue=default,high
directory=/mnt/g/Workplace/explore/php/blog/
autostart=true
startsecs=3
autorestart=true
user=kael
numprocs=8
priority=999
redirect_stderr=true
stdout_logfile=/mnt/g/Workplace/explore/php/blog/storage/supervisor.log
stderr_logfile=/mnt/g/Workplace/explore/php/blog/storage/supervisor.err.log
stdout_logfile_maxbytes=2MB
stderr_logfile_maxbytes=2MB


sudo supervisord -c /etc/supervisord.conf

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start laravel-worker:*

supervisor常用命令

1
2
3
4
5
6
7
supervisorctl stop program_name	#停止某个进程
supervisorctl start program_name	#启动某个进程
supervisorctl restart program_name	#重启某个进程
sudo supervisorctl status [program_name] #查看(某个)进程
supervisorctl stop all	#停止全部进程
supervisorctl reload	#载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程
supervisorctl update	#根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启
coffee