由於Laravel專案關係,需要使用Supervisor來處理Queue的工作,這裡作個簡單記錄此套件安裝過程

前面先以Ubuntu安裝做基礎介紹,結尾會再補充CentOS的部分

Ubuntu篇

Ubuntu 18.04.3版本下操作

安裝supervisor

sudo apt install supervisor

安裝完成後,會在etc下產生supervisor的資料夾

root@linkira-virtual-machine:/etc# ls -la | grep supervisor
drwxr-xr-x   3 root root    4096  1月 14 11:38 supervisor

而進入後打開supervisor.conf內容是這樣

root@linkira-virtual-machine:/etc/supervisor# cat 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

看到include這段,我們要把所有的排程設定檔放至conf.d下,且副檔名為conf

要注意不同系統,副檔名不一樣
CentOS - ini
Ubuntu - conf

我們把一段簡單的排程工作寫進conf.d

[program:test] # 為此排程命名為test,啟動及關閉排程時所認定的名稱
command=bash -i -c "echo $(date) && sleep 5" # 撰寫執行指令
autostart=true # 自動執行
autorestart=true # 中斷時自動重啟
stderr_logfile=/var/log/test.err.log # 輸出 錯誤log
stdout_logfile=/var/log/test.out.log # 輸出 排程log
user=linkira # 選擇執行的帳號
environment=HOME="/home/linkira",USER="linkira" # 設置執行環境及USER

[supervisord]
logfile_maxbytes=4294967296 # 設定log檔大小,系統將會自動rotated

到這裡基本上已經完成了

我們只要給他reload,排程就會開始執行

root@linkira-virtual-machine:/etc/supervisor/conf.d# supervisorctl reload
Restarted supervisord

root@linkira-virtual-machine:/etc/supervisor/conf.d# supervisorctl status
test                             RUNNING   pid 7385, uptime 0:00:02

我們來檢查一下log看看執行狀況

root@linkira-virtual-machine:/etc/supervisor/conf.d# tail -f /var/log/test.out.log
二 1月 14 12:06:46 CST 2020
二 1月 14 12:06:53 CST 2020
二 1月 14 12:06:59 CST 2020

假設我要變更輸出內容在前面增加一段文字

command=bash -i -c "echo Hello Log $(date) && sleep 5"

然而supervisor需要重新讀取conf,才會產生後續追加的文字

root@linkira-virtual-machine:/etc/supervisor/conf.d# supervisorctl reread
test: changed

root@linkira-virtual-machine:/etc/supervisor/conf.d# supervisorctl update
test: stopped
test: updated process group

root@linkira-virtual-machine:/etc/supervisor/conf.d# tail -f /var/log/test.out.log
Hello Log 二 1月 14 12:24:41 CST 2020
Hello Log 二 1月 14 12:24:47 CST 2020
Hello Log 二 1月 14 12:24:53 CST 2020
Hello Log 二 1月 14 12:24:59 CST 2020

最後來個指令總結

supervisorctl reload # 重啟整個supervisor,若有設定自動執行的排程將會啟動

supervisorctl reread  # 檢查子排程是否有變更
supervisorctl update # 自動更新至最新排程設定

supervisorctl status # 檢查排程狀況
supervisorctl start {name} # 啟動指定排程
supervisorctl stop {name}  # 關閉指定排程
supervisorctl restart {name} # 重啟指定排程

btw,我是以root執行所有supervisor指令,一般user的話記得使用sudo


CentOS篇

CentOS Linux release 7.6.1810 版本下操作

首先一樣先安裝supervisor

yum -y install epel-release # 新主機的話請先安裝這包

yum -y install supervisor 

安裝後到etc檢查一下目錄,會發現跟ubuntu的稍微不太一樣

[root@localhost etc]# ls -la | grep supervisord
-rw-r--r--.  1 root root     7953  7月 29  2017 supervisord.conf
drwxr-xr-x.  2 root root       22  1月 14 14:33 supervisord.d

這次我們也檢查看看conf檔中include的內容

[include]
files = supervisord.d/*.ini # 在ubuntu篇提到的,兩個系統的副檔名是不一樣的

到這裡可能大家都熟悉怎麼後續操作了,當排程檔案設定完後,接著要啟動supervisor時,卻報出了錯誤

error: <class 'socket.error'>, [Errno 2] No such file or directory: file: /usr/lib64/python2.7/socket.py line: 224

在CentOS時,我們要多做一個指定conf檔案的動作,這樣才可以正常執行

supervisord -c /etc/supervisord.conf # 請以root執行,並設定正確的conf路徑

剩下操作都跟ubuntu一樣了,這樣就大功告成囉~

可參考文件:Supervisor