Supervisor 使用總結

tsin發表於2019-11-20

說明

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