supervisor 使用系列之一

supervisor 使用系列之一

前幾年本身用PHP寫過一個服務守護的腳本,初步實現了被守護腳本的狀態監控、優雅殺死、以及自動重啓的功能。面試的時候也有問到,爲何不使用supervisor這個工具。由於當時項目少,並未思考那麼多。目前項目中有使用supervisor 做爲swoole 微服務的守護存在,所以準備深刻的學習下,可以實現微服務中的,服務狀態監控功能。python

安裝

1:easy_install 安裝:
easy_install supervisor

2:pip 安裝:
pip install supervisor

3:Debian / Ubuntu能夠直接經過apt安裝:
apt-get install supervisor

配置文件詳解

經過apt-get 安裝的配置文件在:web

/etc/supervisor/supervisord.conf面試

其守護的進程,都轉換微supervisor所管理的子進程,配置文件在:ubuntu

/etc/supervisor/conf.d安全

supervisord.conf 配置項說明

[unix_http_server]
file=/tmp/supervisor.sock   ; UNIX socket 文件,supervisorctl 會使用
;chmod=0700                 ; socket 文件的 mode,默認是 0700
;chown=nobody:nogroup       ; socket 文件的 owner,格式: uid:gid
 
;[inet_http_server]         ; HTTP 服務器,提供 web 管理界面
;port=127.0.0.1:9001        ; Web 管理後臺運行的 IP 和端口,若是開放到公網,須要注意安全性
;username=user              ; 登陸管理後臺的用戶名
;password=123               ; 登陸管理後臺的密碼
 
[supervisord]
logfile=/tmp/supervisord.log ; 日誌文件,默認是 $CWD/supervisord.log
logfile_maxbytes=50MB        ; 日誌文件大小,超出會 rotate,默認 50MB
logfile_backups=10           ; 日誌文件保留備份數量默認 10
loglevel=info                ; 日誌級別,默認 info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ; pid 文件
nodaemon=false               ; 是否在前臺啓動,默認是 false,即以 daemon 的方式啓動
minfds=1024                  ; 能夠打開的文件描述符的最小值,默認 1024
minprocs=200                 ; 能夠打開的進程數的最小值,默認 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
 
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; 經過 UNIX socket 鏈接 supervisord,路徑與 unix_http_server 部分的 file 一致
;serverurl=http://127.0.0.1:9001 ; 經過 HTTP 的方式鏈接 supervisord
 
; 包含其餘的配置文件
[include]
files = relative/directory/*.ini    ; 能夠是 *.conf 或 *.ini

conf.d 下配置文件說明

這個下面的文件配置,以.conf結尾,命名規則微:test.confbash

[program:app] ; 程序名稱,在 supervisorctl 中經過這個值來對程序進行一系列的操做
autorestart=True      ; 程序異常退出後自動重啓
autostart=True        ; 在 supervisord 啓動的時候也自動啓動
redirect_stderr=True  ; 把 stderr 重定向到 stdout,默認 false
environment=PATH="/home/app_env/bin"  ; 能夠經過 environment 來添加須要的環境變量,一種常見的用法是使用指定的 virtualenv 環境
command=python server.py  ; 啓動命令,與手動在命令行啓動的命令是同樣的
user=ubuntu           ; 用哪一個用戶啓動
directory=/home/app/  ; 程序的啓動目錄

啓動

sudo supervisord -c /etc/supervisor/supervisord.conf

經常使用管理

sudo supervisorctl
> status    # 查看程序狀態
> stop usercenter   # 關閉 usercenter 程序
> start usercenter  # 啓動 usercenter 程序
> restart usercenter    # 重啓 usercenter 程序
> reread    # 讀取有更新(增長)的配置文件,不會啓動新添加的程序
> update    # 重啓配置文件修改過的程序
相關文章
相關標籤/搜索