1.安装:supervisor
命令:brew install supervisor
安装日志:
brew install supervisor
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles/su
######################################################################## 100.0%
==> Pouring supervisor-3.3.4.high_sierra.bottle.tar.gz
==> Caveats
To have launchd start supervisor now and restart at login:
brew services start supervisor
Or, if you don't want/need a background service you can just run:
supervisord -c /usr/local/etc/supervisord.ini
==> Summary
? /usr/local/Cellar/supervisor/3.3.4: 526 files, 6.5MB
2.配置supervisord.conf
supervisord 提供了一个模板文件,执行下面shell文件或者输出到文件中,改改就可以用了
命令:cd /usr/local/Cellar/supervisor/3.3.4/bin
执行下面命令:
echo_supervisord_conf (生成配置输出在控制台 /opt/homebrew/etc/supervisor.d/*.ini)
#或者
echo_supervisord_conf > supervisord.conf
打开supervisord.conf文件,写入自定义配置:
[program:theprogramname]
; queue:work --daemon 同listen一样, 只要运行着,就能一直接受请求,不一样的地方是在这个运行模式下,当新的请求到来的时候,不重新加载整个框架,而是直接 fire 动作。能看出来, queue:work --daemon 是最高级的,一般推荐使用这个来处理队列监听
command=php /dir/.../artisan queue:work --daemon
; 自动启动
autostart=true
; 如果在运行后退出,则何时重新启动(def:意外)[false: 进程将不会自动重新启动; true: 则进程将在退出时无条件重新启动; unexpected: 如果发生意外,当程序退出时,退出代码不是与该进程配置相关联的退出代码之一时]
autorestart=true
; 将uid设置为此UNIX帐户以运行程序(启动用户)
user=root
stdout_logfile=/dir/.../stdout.log
stderr_logfile=/dir/.../stderr.log
; Signal Description
; SIGTERM The TERM signal is sent to a process to request its termination. Unlike the KILL signal, it can be caught and interpreted or ignored by the process. This signal allows the process to perform nice termination releasing resources and saving state if appropriate. It should be noted that SIGINT is nearly identical to SIGTERM.
; SIGHUP The HUP signal is sent to a process when its controlling terminal is closed. It was originally designed to notify a serial line drop (HUP stands for "Hang Up"). In modern systems, this signal usually indicates the controlling pseudo or virtual terminal is closed.
; SIGINT The INT signal is sent to a process by its controlling terminal when a user wants to interrupt the process. This signal is often initiated by pressing Ctrl+C, but on some systems, the "delete" character or "break" key can be used.
; SIGQUIT The QUIT signal is sent to a process by its controlling terminal when the user requests that the process perform a core dump.
; SIGKILL Forcefully terminate a process. With STOP, this is one of two signals which cannot be intercepted, ignored, or handled by the process itself.
; SIGUSR1 User-defined signal 1. This is one of two signals designated for custom user signal handling.
; SIGUSR2 User-defined signal 2. This is one of two signals designated for custom user signal handling.
; INT 通过Ctrl+C停止进程
stopsignal=INT
配置完。
启动:
根据安装日志我们可以看到这样描述:
if you don't want/need a background service you can just run:
supervisord -c /usr/local/etc/supervisord.ini
那么此时可以执行命令:supervisord -c /usr/local/etc/supervisord.ini 启动服务了。
本机当前处于 /usr/local/Cellar/supervisor/3.3.4/bin
所以我直接运行 supervisord -c supervisord.conf
查看服务运行状态:ps -ef | grep python
注意文件在结尾处:
[include]
files = /usr/local/etc/supervisor.d/*.ini
这个配置自动加载了.ini文件,
若要部署多台服务器都要部署监控队列服务,我们可以在这个位置添加其他的配置项。
目录:/usr/local/etc
在/usr/local/etc/supervisord.ini同级目录里添加新配置,这里我仅添加worker.ini测试:
配置:supervisord.ini
[program:eduamin]
command=php /Applications/MAMP/htdocs/edu.amin/artisan
process_name=%(program_name)s
autostart=true
autorestart=true
user=root
redirect_stderr=true
stdout_logfile=/dir/.../stdout.log
配置:worker.ini
[program:eduamin-bg]
process_name=%(program_name)s_%(process_num)02d
command=php /dir/.../artisan queue:work redis --queue=test_a,test_b --sleep=3 --tries=3
autostart=true
autorestart=true
user=root
numprocs=8
stdout_logfile=/dir/.../stdout.log
配置完成。