supervisor 使用系列之一

supervisor 使用系列之一

前几年自己用PHP写过一个服务守护的脚本,初步实现了被守护脚本的状态监控、优雅杀死、以及自动重启的功能。面试的时候也有问到,为什么不使用supervisor这个工具。因为当时项目少,并未思考那么多。目前项目中有使用supervisor 作为swoole 微服务的守护存在,因此准备深入的学习下,能够实现微服务中的,服务状态监控功能。

安装

1:easy_install 安装:
easy_install supervisor

2:pip 安装:
pip install supervisor

3:Debian / Ubuntu可以直接通过apt安装:
apt-get install supervisor

配置文件详解

通过apt-get 安装的配置文件在:

/etc/supervisor/supervisord.conf

其守护的进程,都转换微supervisor所管理的子进程,配置文件在:

/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.conf

[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    # 重启配置文件修改过的程序
posted @ 2019-03-22 14:46  roverliang  阅读(342)  评论(0编辑  收藏  举报