Supervisor 使用总结

说明

Supervisor是一个进程监护工具,在Laravel中,我们用来监护消息队列、Horizon进程,以便在其意外退出时自动重启。这里以教程L02 第5.9节中的Horizon监护为例。

安装和配置

Ubuntu环境下,运行:apt-get install -y supervisor。安装完毕后,配置文件位于:/etc/supervior,在该文件夹下,有:

conf.d  # 自定义配置文件存放目录
supervisord.conf # 主配置文件,自定义文件会在这里include进来

打开supervisord.conf ,内容如下:

; 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  ;自定义的配置在这里包含进来

为了监护Horizon进程,我们在conf.d文件夹下创建一份配置,文件名随意,各项作用见注释:

[program:laravel_horizon]    ;监护程序名称,随意起,但不能跟其他的重复
process_name=%(program_name)s_%(process_num)02d    ;进程名称
directory=/var/www/html/larabbs    ;命令运行的目录
command=php artisan horizon    ;要执行的命令
autostart=true    ;当supervisor启动时,程序自动启动
autorestart=true    ;自动重启
numprocs=1    ; 进程数
user=root     ;执行命令的账号
stopasgroup=true    ;这个和下面一个配置可以防止监护的进程意外重启后子进程残留
killasgroup=true
redirect_stderr=true    ;这里设为true,就可以配置下面的目录
stdout_logfile=/var/www/html/larabbs/storage/laravel_horizon.log    ;日志目录

使用

运行supervisord -c /etc/supervisor/supervisord.conf 启动,启动后就可以使用supervisorctl命令来进行一些进程管理操作,比如:

 # 查看状态,比如,在本例子中,将会输出:laravel_horizon:laravel_horizon_00   RUNNING   pid 62, uptime 1 day, 7:11:04
 supervisorctl status 

 # 以下针对laravel_horizon:laravel_horizon_00进程操作:
 supervisorctl stop laravel_horizon:laravel_horizon_00 
 supervisorctl start laravel_horizon:laravel_horizon_00
 supervisorctl restart laravel_horizon:laravel_horizon_00

 # 关闭supervisor
 supervisorctl shutdown

如果supervisor成功启动,在日志文件中可以看到:Horizon started successfully.,当有队列被执行了,日志中可以看到信息,比如:

[2019-11-19 14:27:30][8] Processing: App\Jobs\TranslateSlug
[2019-11-19 14:27:30][8] Processed:  App\Jobs\TranslateSlug

遇到的问题

  • 启动时报错,找不到unix:///var/run/supervisor.sock

    解决:Linux命令中断依次运行以下命令:

    touch /var/run/supervisor.sock
    chmod 777 /var/run/supervisor.sock
  • 启动时报错:Unlinking stale socket /var/run/supervisor.sock

    解决:运行:unlink /var/run/supervisor.sock

  • supervisor: couldn't chdir to /var/html/www/larabbs: ENOENT
    supervisor: child process was not spawned

    解决:directory目录路径写错了,调整回正确路径

参考

本作品采用《CC 协议》,转载必须注明作者和本文链接
Was mich nicht umbringt, macht mich stärker
本帖由系统于 1年前 自动加精
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 3

aptyum 安装的使用 systemctl/service 启动就可以了,启动脚本已经写好了,不用自己去运行程序本身

4年前 评论

@minororange 我是用docker容器运行的,service supervisord status 可以,systemctl容器中没有

4年前 评论

我启动的时候一直报错Please make sure the PHP Redis extension is installed and enabled,,我看了php-m里有redis

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!