【注意】最后更新于 January 10, 2020,文中内容可能已过时,请谨慎使用。
环境介绍
- Ubuntu 5.4.0-6ubuntu1~16.04.9
安装 Supervisor
1
| sudo apt-get install supervisor
|
安装后可以查看到 supervisor 已经启动
1
2
3
| ➜ ~ ps aux|grep supervisor
root 14314 0.9 0.2 58936 18572 ? Ss 16:13 0:00 /usr/bin/python /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
root 14358 0.0 0.0 14228 908 pts/2 S+ 16:13 0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn supervisor
|
配置 Supervisor
我的supervisord.conf
配置文件,cat /etc/supervisor/supervisord.conf
如下:
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
| ; 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
|
这里我以为mooncake-queue-worker.conf
为例,命令:vim /etc/supervisor/conf.d/mooncake-queue-worker.conf
配置清单如下:
1
2
3
4
5
6
7
8
9
10
| [program:mooncake-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /project/artisan queue:work redis --queue=access
autostart=true
autorestart=true
user=www-data
numprocs=4
redirect_stderr=true
stdout_logfile=/run/log/moonkcake-queue-worker.log
|
注意:
project
换成自己的项目路径user=www-data
配置成自己的php
进程用户numprocs
是启动进程数
启动 Supervisor
1
2
| ➜ ~ sudo supervisorctl reread
mooncake-worker: available
|
1
2
| ➜ ~ sudo supervisorctl update
mooncake-worker: added process group
|
此时再查看一下php
进程,可以成功看到4个Queue启动了
1
2
3
4
5
6
7
| ➜ ~ ps aux|grep php
www-data 9933 0.0 0.8 466168 68760 ? S 11:55 0:03 php-fpm: pool www
www-data 14383 0.3 0.8 558056 71696 ? S 16:15 0:01 php-fpm: pool www
www-data 14508 1.2 0.6 335088 49780 ? S 16:24 0:00 php /usr/share/nginx/neox-mooncake-sjms/artisan queue:work redis --queue=access
www-data 14509 1.3 0.6 335088 49648 ? S 16:24 0:00 php /usr/share/nginx/neox-mooncake-sjms/artisan queue:work redis --queue=access
www-data 14510 1.5 0.6 337136 52176 ? S 16:24 0:00 php /usr/share/nginx/neox-mooncake-sjms/artisan queue:work redis --queue=access
www-data 14511 1.7 0.6 337136 52228 ? S 16:24 0:00 php /usr/share/nginx/neox-mooncake-sjms/artisan queue:work redis --queue=access
|
常用命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| # 停止某个进程
supervisorctl stop program_name
# 启动某个进程
supervisorctl start program_name
# 重启某个进程
supervisorctl restart program_name
# 停止全部进程
supervisorctl stop all
# 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程
supervisorctl reload
# 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启
supervisorctl update
# 杀死进程
kill supervisor-pid(查询得知 ps aux|grep super)
# 重启监听
/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
|
Centos 中使用 Supervisor
安装
```bash
yum install -y epel-release
yum install -y supervisor
1
2
3
4
5
6
7
8
9
10
|
### 常用命令
```bash
supervisorctl status:查看所有进程的状态
supervisorctl stop :停止
supervisorctl start :启动
supervisorctl restart : 重启
supervisorctl update :配置文件修改后可以使用该命令加载新的配置
supervisorctl reload: 重新启动配置中的所有程序
|
Centos 中使用 Supervisor 文档来自孟昭利反馈
常见问题
Q1. Invalid user name
1
2
| ➜ ~ sudo supervisorctl reread
ERROR: CANT_REREAD: Invalid user name www in section 'program:mooncake-worker' (file: '/etc/supervisor/conf.d/mooncake-queue-worker.conf')
|
A1:查看当前PHP
进程用户
1
2
3
| ➜ nginx ps aux|grep php
www-data 1736 0.0 0.9 540960 79144 ? S Jan09 0:30 php-fpm: pool www
www-data 2114 0.0 0.8 537876 73172 ? S Jan09 0:30 php-fpm: pool www
|
修改mooncake-queue-worker.conf
中user
项为user=www-data
。
Q2. 启动:sudo supervisorctl reread,报错如下:
1
| error: <class 'socket.error'>, [Errno 2] No such file or directory: file: /usr/lib64/python2.7/socket.py line: 224
|
A2. 解决办法,输入supervisord
获取到提示信息:/usr/lib/python2.7/site-packages/supervisor/options.py:461: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a “-c” argument specifying an absolute path to a configuration file for improved security.Supervisord is running as root and it is searching 很明显的看到提示信息 需要指定一个“-c”参数来指定配置文件的绝对路径。
所以使用supervisord -c /etc/supervisor/supervisord.conf
来启动
Q3. 解决unix:///tmp/supervisor.sock no such file 的问题
A3. 参考地址:https://blog.csdn.net/qq_28885149/article/details/79364685
参考地址