[工具&筆記]supervisor

supervisor是一個在類UNIX操作系統(tǒng)下,以C/S架構(gòu)來對進程進行管理的工具。

參考:Supervisor: A Process Control System使用 supervisor 管理進程

組成

    1. 服務(wù)端(Server)
      supervisord
    1. 命令行客戶端(Client)
      supervisorctl
    1. Web界面(Client)
    1. XML-RPC接口(Client)
      詳見XML-RPC API Documentation

配置

安裝好后,可以執(zhí)行命令echo_supervisord_conf來獲取默認(rèn)的配置項,進行參考。
由于服務(wù)端和客戶端使用的是同一個配置文件,所以在配置文件中包含它們各自的配置項,除此之外,還包含對應(yīng)被supervisor管理的應(yīng)用的配置。
配置文件一般命名為supervisord.conf,其中使用 ; 對配置項進行注釋。

    1. supervisord配置
[unix_http_server]
file=/tmp/supervisor.sock   ; UNIX socket 文件,supervisorctl 會使用
;chmod=0700                 ; socket 文件的 mode,默認(rèn)是 0700
;chown=nobody:nogroup       ; socket 文件的 owner,格式: uid:gid

[inet_http_server]         ; HTTP 服務(wù)器,提供 web 管理界面
port=127.0.0.1:9001        ; Web 管理后臺運行的 IP 和端口,如果開放到公網(wǎng),需要注意安全性
;username=user              ; 登錄管理后臺的用戶名
;password=123               ; 登錄管理后臺的密碼

[supervisord]
logfile=/tmp/supervisord.log ; 日志文件,默認(rèn)是 $CWD/supervisord.log
logfile_maxbytes=50MB        ; 日志文件大小,超出會 rotate,默認(rèn) 50MB
logfile_backups=10           ; 日志文件保留備份數(shù)量默認(rèn) 10
loglevel=info                ; 日志級別,默認(rèn) info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ; pid 文件
nodaemon=true               ; 是否在前臺啟動,默認(rèn)是 false,即以 daemon 的方式啟動
minfds=1024                  ; 可以打開的文件描述符的最小值,默認(rèn) 1024
minprocs=200                 ; 可以打開的進程數(shù)的最小值,默認(rèn) 200

; 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

; 包含其他的配置文件
;[include]
;files = relative/directory/*.ini    ; 可以是 *.conf 或 *.ini

其中inet_http_server需要時才配置。
nodamon配置項決定是否以 daemon 的方式啟動。
其他一般不變。

    1. supervisorctl配置
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; 通過 UNIX socket 連接 supervisord,路徑與 unix_http_server 部分的 file 一致
serverurl=http://127.0.0.1:9001 ; 通過 HTTP 的方式連接 supervisord

由于我希望使用Web界面,所以在這里給了兩種連接方式。

    1. 應(yīng)用配置
[program:cat]
command=/bin/cat

[program:redis-server]
command=/usr/local/bin/redis-server
priority=500
autostart=true
autorestart=true
startretries=3
exitcodes=0
stdout_logfile=/tmp/redis-server/server.log
stderr_logfile=/tmp/redis-server/server.err
stdout_logfile_maxbytes=5MB
stderr_logfile_maxbytes=5MB
stdout_logfile_backups=10
stderr_logfile_backups=10

這里我啟動了兩個應(yīng)用(program),program冒號后為定義的標(biāo)識,須唯一。
主要的是定義 command ,即執(zhí)行的命令。若對啟動目錄有要求,則添加 directory 項。
proority小的應(yīng)用啟動時提前,殺死時滯后。反之則反。

命令

    1. 啟動supervisord
      supervisord -c supervisord.conf
      查詢是否運行:ps aux | grep supervisord
      并沒有提供停止功能。
    1. 啟動supervisorctl
      supervisorctl -c supervisord.conf
      以上兩個命令均有自動尋找配置文件的功能,可以不加 -c 。
      執(zhí)行之后,進入supervisorctl shell,如果不想進入,可以直接在supervisorctl后跟以下幾個命令:status, stop xx, start xx, restart xx/all, reread, update, exit。
      其中reread只是重新獲取配置文件,update才是依據(jù)配置文件對應(yīng)用進行操作。
    1. web界面
      瀏覽設(shè)置的 http serverurl 即可。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容