CentOS supervisor 安裝與配置 (Laravel 佇列示例)

Wsmallnews發表於2019-05-25

參考文件

https://learnku.com/laravel/t/2126/supervisor-installation-configuration-use

安裝

yum install supervisor

配置檔案目錄

/etc/
    supervisor.conf
    supervisor.d/
        專案一.ini
        專案二.ini

laravel 佇列配置示例

說明, user=www 為啟動的 php artisan 程式執行使用者,這個要和 php 執行使用者使用一個,而supervisor 啟動使用者為 root, 配置在 supervisor.conf 檔案中,stdout 等日誌檔案建立者均為 supervisor 使用者,即 root

supervisor 主配置檔案路徑為/var/log/supervisor/supervisord.log(版本間可能不同,請檢視主配置檔案/etc/supervisor.conf logfile 配置項),寫 stdout 等日誌路徑時,可使用 /var/log/supervisor/{programname 即 smallnews}.log

[program:smallnews]
process_name=%(program_name)s_%(process_num)02d
command=/usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon
numprocs=3
autostart=true
autorestart=true
user=www
redirect_stderr=true
stdout_logfile=/var/log/supervisor/smallnews.log
stderr_logfile=/var/log/supervisor/smallnews_err.log

啟動與停止

stop 之後相關的程式全部被殺死,restart 相關程式全部重啟

開機自啟動

systemctl enable supervisord

啟動停止重啟

systemctl start|stop|restart supervisord

狀態

systemctl status supervisord

狀態資訊

正在執行

● supervisord.service - Process Monitoring and Control Daemon
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-05-25 17:53:33 CST; 1min 34s ago
  Process: 20182 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)
 Main PID: 20185 (supervisord)
   CGroup: /system.slice/supervisord.service
           ├─20185 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
           ├─20186 /usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon
           ├─20187 /usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon
           └─20188 /usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon

停止執行

● supervisord.service - Process Monitoring and Control Daemon
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

檢視啟動的程式

主程式
#> ps -ef | grep supervisor
root     23668     1  0 18:29 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf

子程式 即 supervisor 堅守的程式
#> ps -ef | grep artisan
www      23669 23668  0 18:29 ?        00:00:00 /usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon
www      23670 23668  0 18:29 ?        00:00:00 /usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon
www      23671 23668  0 18:29 ?        00:00:00 /usr/local/php/bin/php /data/html/smallnews/artisan queue:work database --sleep=3 --tries=3 --daemon

網頁檢視狀態

[inet_http_server]         ; inet (TCP) server disabled by default
port=0.0.0.0:9001          ; (ip_address:port specifier, *:port for all iface)
username=user              ; 使用者名稱 (default is no username (open server))
password=123               ; 密碼 (default is no password (open server))

訪問網址檢視 Supervisor status,可直接操作啟動,重啟,停止等指令

ip:9001 
賬號:user
密碼:123
State Description Name Action
running pid 22506, uptime 0:00:28 smallnews:smallnews_00 Restart Stop Clear Log Tail -f
running pid 22505, uptime 0:00:28 smallnews:smallnews_01 Restart Stop Clear Log Tail -f
running pid 22504, uptime 0:00:28 smallnews:smallnews_02 Restart Stop Clear Log Tail -f

配置項說明

摘取至上面文件

;*為必須填寫項
;*[program:應用名稱]
[program:cat]
;*命令路徑,如果使用python啟動的程式應該為 python /home/test.py, 
;不建議放入/home/user/, 對於非user使用者一般情況下是不能訪問
command=/bin/cat
;當numprocs為1時,process_name=%(program_name)s
;當numprocs>=2時,%(program_name)s_%(process_num)02d
process_name=%(program_name)s
;程式數量
numprocs=1
;執行目錄,若有/home/supervisor_test/test1.py
;將directory設定成/home/supervisor_test
;則command只需設定成python test1.py
;否則command必須設定成絕對執行目錄
directory=/tmp
;掩碼:--- -w- -w-, 轉換後rwx r-x w-x
umask=022
;優先順序,值越高,最後啟動,最先被關閉,預設值999
priority=999
;如果是true,當supervisor啟動時,程式將會自動啟動
autostart=true
;*自動重啟
autorestart=true
;啟動延時執行,預設1秒
startsecs=10
;啟動嘗試次數,預設3次
startretries=3
;當退出碼是0,2時,執行重啟,預設值0,2
exitcodes=0,2
;停止訊號,預設TERM
;中斷:INT(類似於Ctrl+C)(kill -INT pid),退出後會將寫檔案或日誌(推薦)
;終止:TERM(kill -TERM pid)
;掛起:HUP(kill -HUP pid),注意與Ctrl+Z/kill -stop pid不同
;從容停止:QUIT(kill -QUIT pid)
;KILL, USR1, USR2其他見命令(kill -l),說明1
stopsignal=TERM
stopwaitsecs=10
;*以root使用者執行,如果是 php 相關程式,請使用 php 啟動使用者
user=root
;重定向
redirect_stderr=false
stdout_logfile=/a/path
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stderr_logfile=/a/path
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB
;環境變數設定
environment=A="1",B="2"
serverurl=AUTO

常見問題

問題一 啟動報錯

描述

#> systemctl restart supervisord

Job for supervisord.service failed because the control process exited with error code. See "systemctl status supervisord.service" and "journalctl -xe" for details

#> systemctl status supervisord.service

supervisord.service - Process Monitoring and Control Daemon
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Sat 2019-05-25 18:23:46 CST; 13s ago
  Process: 23126 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=2)
 Main PID: 22502 (code=exited, status=0/SUCCESS)

May 25 18:23:46 VM_115_62_centos systemd[1]: Starting Process Monitoring and Control Daemon...
May 25 18:23:46 VM_115_62_centos supervisord[23126]: Error: section [inet_http_server] has no port value
May 25 18:23:46 VM_115_62_centos supervisord[23126]: For help, use /usr/bin/supervisord -h
May 25 18:23:46 VM_115_62_centos systemd[1]: supervisord.service: control process exited, code=exited status=2
May 25 18:23:46 VM_115_62_centos systemd[1]: Failed to start Process Monitoring and Control Daemon.
May 25 18:23:46 VM_115_62_centos systemd[1]: Unit supervisord.service entered failed state.
May 25 18:23:46 VM_115_62_centos systemd[1]: supervisord.service failed.

從上面得出報錯資訊為

Error: section [inet_http_server] has no port value

解決

如果存在 [inet_http_server] 則必須存在 127.0.0.1::9001 或者 0.0.0.0:9001,即必須啟動這個監聽程式

如果想關掉 必須把 [inet_http_server] 註釋掉,即最前面加 “;”

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章