β

使用supervisord管理进程

Linux运维网 671 阅读

0x00 Supervisor简介

————————————————————
Supervisor是用Python实现的一款非常实用的进程管理工具,可以方便的通过命令开启、关闭、重启等操作,而且它管理的进程一旦崩溃会自动重启,但是supervisor管理的进程必须由supervisord来启动,并且管理的程序必要是非Daemon程序,Supervisor会帮你把它转化为Daemon程序,比如想要使用Supervisor来管理Nginx进程,就必须在Nginx配置文件中加入 daemon off让Nginx以非Daemon方式运行

0x01 Supervisor安装

————————————————————
Supervisor是基于Python编写,所以可以使用easy_install或 pip来安装,这里使用pip

[root@localhost ~]# yum install python-pip -y
[root@localhost ~]# pip install supervisor
Downloading/unpacking supervisor
  Downloading supervisor-3.0.tar.gz (459kB): 459kB downloaded
  Running setup.py egg_info for package supervisor
Requirement already satisfied (use --upgrade to upgrade): distribute in /usr/lib/python2.6/site-packages (from supervisor)
Downloading/unpacking meld3>=0.6.5 (from supervisor)
  Downloading meld3-0.6.10.tar.gz (41kB): 41kB downloaded
  Running setup.py egg_info for package meld3
Installing collected packages: supervisor, meld3
  Running setup.py install for supervisor
    Skipping installation of /usr/lib/python2.6/site-packages/supervisor/__init__.py (namespace package)
    Installing /usr/lib/python2.6/site-packages/supervisor-3.0-py2.6-nspkg.pth
    Installing echo_supervisord_conf script to /usr/bin
    Installing pidproxy script to /usr/bin
    Installing supervisorctl script to /usr/bin
    Installing supervisord script to /usr/bin
  Running setup.py install for meld3
Successfully installed supervisor meld3
Cleaning up...

安装完成后,supervisor会提供两个命令:
supervisord: 启动supervisor守护进程
supervisorctl: supervisor控制脚本,类似与 service 命令

0x02 Supervisor配置
————————————————————
生成配置文件:

[root@localhost ~]# echo_supervisord_conf > /etc/supervisord.conf
[root@localhost ~]# vim /etc/supervisord.conf
[unix_http_server]
; 定义Socket文件位置
file=/tmp/supervisor.sock

; WEB管理界面,默认关闭
[inet_http_server]
port=9100   ; 默认 127.0.0.1:9001
username=tux
password=123 

[supervisord]
logfile=/var/log/supervisord.log
logfile_maxbytes=50MB  ; 单个日志大小, 默认 50M, 超过指定大小则进行日志轮询
logfile_backups=10      ; 保留日志数据
loglevel=info   ; 可选 debug,warn,trace
pidfile=/var/run/supervisord.pid
nodaemon=false
minfds=1024
minprocs=200

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
; Socket 文件路径,必须与 unix_http_server中 file 位置一致
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket

; 管理单个进程配置, program 后面是服务名称
[program:flask]
command=/opt/verystar/flask/bin/python runserver.py              
process_name=%(program_name)s                   ; 程序名称
directory=/opt/verystar/               ; 程序目录,执行命令前会先进行到此目录 
autostart=true                ; supervisord启动时是否自动启动此进程
autorestart=true            ; 发生故障时自动重启
startsecs=5                   ; 5秒内程序必须启动, 默认 1秒
startretries=3                ; 最多 3 次启动失败
stopsignal=QUIT               ; 使用QUIT信号来结束进程,默认: TERM
redirect_stderr=true          ; 将 stderr 重定向到 stdout
stdout_logfile=/var/log/flask-access.log

0x03 简单flask测试脚本

————————————————————
[root@localhost verystar]# vim /opt/verystar/runserver.py

#!/opt/verystar/flask/bin/python
# -*- coding: utf-8 -*-
from flask import Flask
app = Flask(__name__)

@app.route('/')
@app.route('/<name>')
def index(name=None):
    if not name:
        name = 'World'
    return "Hello,%s" % name

if __name__ == "__main__":
    app.run(host='0.0.0.0')

0x04 Supervisor启动及进程管理

————————————————————
启动supervisord守护进程

[root@localhost verystar]# supervisord -c /etc/supervisord.conf
[root@localhost verystar]# supervisorctl status flask
flask                            RUNNING    pid 13306, uptime 0:00:20
[root@localhost verystar]# supervisorctl stop flask
flask: stopped
[root@localhost verystar]# supervisorctl status 
flask                            STOPPED    Nov 27 01:04 PM
[root@localhost verystar]# supervisorctl start flask
flask: started
[root@localhost verystar]# supervisorctl reload flask
Restarted supervisord
[root@localhost verystar]# curl http://localhost:5000/
Hello,World
[root@localhost verystar]# curl http://localhost:5000/tux
Hello,tux
测试: 将flask进程Kill掉,看supervisor是否能将其重启
[root@localhost verystar]# kill 13306
[root@localhost verystar]# supervisorctl status flask
flask                            STARTING   
[root@localhost verystar]# supervisorctl status flask
flask                            RUNNING    pid 13331, uptime 0:00:08

简单的WEB管理界面
supervisor

作者:Linux运维网
Linux运维|Mysql|Python|Shell|虚拟化|集群
原文地址:使用supervisord管理进程, 感谢原作者分享。

发表评论