Supervisor 筆記


建立時間: 2024年4月6日 09:12
更新時間: 2024年6月17日 16:39

說明

Supervisor 是一個客戶端/伺服器系統,可讓使用者控制類 UNIX 作業系統上的多個程序。

本篇會紀錄一些常用的指令和配置,讓開發人員可以快速上手。

安裝

在官方文件中,它建議使用 python 套件安裝。

$ pip install supervisor

運作機制

我是採用一個 supervisord 配置檔,以及多個 program 配置檔的方式建立,多個 program 配置檔需要 supervisord 配置檔配置 [include]

supervisord.conf

[include]
files = /etc/supervisor/conf.d/*.conf

如上範例,將要啟動的程式都寫在 /etc/supervisor/conf.d/ 目錄裡,目錄位置請自行調整。

系統會啟動一個 supervisord,接著再執行 conf.d/*conf 所有的程式。

建立配置檔

$ echo_supervisord_conf
將「範例」Supervisor 配置檔輸出到終端。


$ echo_supervisord_conf > /etc/supervisord.conf
將「範例」Supervisor 配置檔寫入到 /etc/supervisord.conf,此位置為官方建議,如果您沒有 root 存取權限,這將無法運行。


$ echo_supervisord_conf > supervisord.conf
如果您沒有 root 存取權限,或者您不想將 supervisord.conf 檔案放入 /etc/supervisord.conf 中,則可以將其放入目前目錄,並使用 -c 標誌啟動 supervisord 以指定配置文件位置。

supervisord 配置檔

此範例為 linux 安裝 supervisor 時自動產生的配置檔,除了 [inet_http_server] 是我自己新增的之外。

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                      ; sockef file mode (default 0700)

; 網頁版 UI 操作
;[inet_http_server]         ; inet (TCP) server disabled by default
;port=127.0.0.1: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)

[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)

[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

[include]
files = /etc/supervisor/conf.d/*.conf

program 配置檔

這裡列出幾項常用的配置項目

;[program:theprogramname]
;command=/bin/cat              ; the program (relative uses PATH, can take args)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;autostart=true                ; start at supervisord start (default: true)
;autorestart=unexpected        ; when to restart if exited after running (def: unexpected)
;user=chrism                   ; setuid to this UNIX account to run the program
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
  • theprogramname: 程式名稱,可自定義
  • command: 啟動的指令
  • directory: 切換當前目錄位置
  • autostart: 啟動 supervisord 時自動啟動
  • autorestart: 程序結束自動重新啟動
  • user: 以哪個使用者身分啟動(只有用 root 啟動 supervisord 才能設定,因為權限不能低於指定的 user)
  • stdout_logfile: 標準輸出記錄檔
  • stderr_logfile: 標準錯誤輸出記錄檔

supervisord 命令

說明

以守護程序執行一組應用程式。

使用

$ supervisord [options]

選項

-c FILE, --configuration=FILE

Supervisord 配置檔的路徑。

範例

$ supervisord -c supervisord.conf
啟動 supervisor,讀取當前資料夾底下的 supervisord.conf

supervisorctl 命令

從終端控制 supervisord 運行的應用程式。

使用

$ supervisorctl [options] [action [arguments]]

動作

pid
取得 supervisord 的 PID。

reload
重新啟動遠端 supervisord。

reread
重新載入守護程序的配置文件,無需新增/刪除(無需重新啟動)。

start <name>
啟動一個程序

status
取得所有程序狀態資訊。

update
重新載入配置並根據需要添加/刪除,並將重新啟動受影響的程序。

取得 process id

找到設定檔的 [supervisord] 部分,pidfile 存放 process id。

pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid

如上,打開 /tmp/supervisord.pid,查看 process id。

訊號處理

使用 kill 命令,對 Supervisord 程序進行處理。

訊號

SIGHUP
Supervisord 將停止所有程序,從它找到的第一個設定檔重新載入配置,然後啟動所有程序。

SIGTERM
Supervisord 及其所有子程序將關閉。這可能需要幾秒鐘。

範例

假設 process id 是 12345

$ kill -s SIGTERM 12345
關閉 Supervisord。

$ kill -s SIGHUP 12345
重新啟動 Supervisord。

參考

觀看次數: 293
managermonitorprocesssupervisor程序管理
按讚追蹤 Enjoy 軟體 Facebook 粉絲專頁
每週分享資訊技術

一杯咖啡的力量,勝過千言萬語的感謝。

支持我一杯咖啡,讓我繼續創作優質內容,與您分享更多知識與樂趣!