supervisor配置与应用

1.简介

supervisor 是一款基于Python的进程管理工具,可以很方便的管理服务器上部署的应用程序。supervisor的功能如下:

  a. 启动、重启、关闭包括但不限于python进程。

  b.查看进程的运行状态。

  c.批量维护多个进程。

2.安装

环境:centos7下

yum install supervisor

2.命令

#启动Supervisor服务
##-c制定让其读取的配置文件
supervisord -c /etc/supervisor/supervisord.conf
#关闭
supervisorctl shutdown

# 重启
supervisorctl reload

 其他常用命令:

(1)添加/删除 要管理服务

  a.添加或删除配置文件 
       b.更新

supervisorctl update

(2)管理supervisor下的服务

###启动服务
supervisorctl start all
supervisorctl start service_name
###关闭服务
supervisorctl stop all
supervisorctl stop service_name
###查看状态
supervisorctl status [service_name]
###重新启动所有服务或者是某个服务
supervisorctl restart all
supervisorctl restart service_name

3.设置开机启动

centos7下:

新建文件supervisord.service

#supervisord.service

[Unit] 
Description=Supervisor daemon

[Service] 
Type=forking 
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf 
ExecStop=/usr/bin/supervisorctl shutdown 
ExecReload=/usr/bin/supervisorctl reload 
KillMode=process 
Restart=on-failure 
RestartSec=42s

[Install] 
WantedBy=multi-user.target

将文件拷贝到/usr/lib/systemd/system/

cp supervisord.service /usr/lib/systemd/system/

启动服务

systemctl enable supervisord

验证一下是否为开机启动

systemctl is-enabled supervisord

4、配置参数说明

[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
View Code

5.demo

下面以监控uwsgi为例

先在etc/supervisord.d目录下创建一个名为uwsgi_statrt.ini的配置文件

[program:uwsgi_start]
directory=/usr/bin  # 运行程序的目录
command=uwsgi --ini  /home/project/uwsgi.ini #运行程序的命令
stopsignal=QUIT     # 杀进程的信号
autostart=true     #意外退出是否自动重启
autorestart=true   # 是否随supervisord启动而启动
startsecs=10        # 进程持续运行多久才认为是启动成功
stopwaitsecs=600     # 向进程发出stopsignal后等待OS向supervisord返回SIGCHILD 的时间。若超时则supervisord将使用SIGKILL杀进程
killasgroup
=true numprocs=1 redirect_stderr=true user=root #用户 stdout_logfile=/var/log/supervisor/uwsgi_start.log #错误日志输出路径
stdout_logfile_maxbytes=100MB 
stdout_logfile_backups
=15
stdout_logfile_maxbytes
=300MB

更新

supervisorctl update

猜你喜欢

转载自www.cnblogs.com/freely/p/10087950.html