【supervisord】部署单进程服务的利器

最近听了一场分享,里边同事介绍了一些python开发环境常用到的几种工具,其中之一就supervisord,分享后自己做了些功课,概括一下supervisord是一个什么东西呢

1. 它是一个独立的常驻内存的可以后台运行的监控,管理其他进程的东西

2. 它可以管理任何进程,掌管他们的生死,记录他们个生死变换的时间,记录他们运行吐出的各种数据,主要就是stdout,stderr的输出

3. 提供了一个很好的web页面管理这些进程的运行,查看他们的日志

 

废话不说了,记录下安装,使用的过程,以备后用

1. 安装

pip-2.7 install supervisor

2. 写一个程序,自己绕着跑的程序 run.sh

#!/usr/bin/bash
# coding: UTF-8


while true
    do
        sleep 1
        echo "hi, fucker"
    done

 

3. 找一个目录,创建supervisor的配置文件 config.conf

[inet_http_server]
port = 0.0.0.0:11316
username = hefei
password = 123456

[program:echo_fucker]
command = bash /home/hefei/codes/mywork/supervisor/run.sh
autostart = true
stdout_logfile = /home/hefei/codes/mywork/supervisor/run.log

[supervisord]
logfile=/home/hefei/codes/mywork/supervisor/supervisord.log ; (main log file;default $CWD/supervisor
d.log)
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (log level;default info; others: debug,warn,trace)
pidfile=/home/hefei/codes/mywork/supervisor/supervisord.pid ; (supervisord pidfile;default superviso
rd.pid)
nodaemon=false              ; (start in foreground if true;default false)
minfds=1024                 ; (min. avail startup file descriptors;default 1024)
minprocs=200                ; (min. avail process descriptors;default 200)
;umask=022                  ; (process file creation umask;default 022)
;user=chrism                 ; (default is current user, required if root)
;identifier=supervisor       ; (supervisord identifier, default is 'supervisor')
;directory=/tmp              ; (default is not to cd during start)
;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value       ; (key value pairs to add to environment)
;strip_ansi=false            ; (strip ansi escape codes in logs; def. false)

; 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]
[supervisorctl]
;serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
serverurl=http://60.28.209.27:11316 ; use an http:// url to specify an inet socket
;username=ktep              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.

 

4. 启动supervisord服务,这是个常驻内存的服务,他死了,supervisor就真的死了

supervisord -c config.conf

这时候查看run.sh是不是真的起来了呢?

ps -ef | grep run.sh | grep -v grep

hefei    14252  9858  0 15:20 ?        00:00:00 bash /home/hefei/codes/mywork/supervisor/run.sh

 

5. 使用supervisorctl ,在命令行下管理进程

supervisorctl -c config.conf start echo_fucker

supervisorctl -c config.conf stop echo_fucker

supervisorctl -c config.conf restart echo_fucker

supervisorctl -c config.conf status echo_fucker

 

[~/codes/mywork/supervisor]$ supervisorctl -c config.conf help

default commands (type help <topic>):
=====================================
add    clear  fg        open  quit    remove  restart   start   stop  update
avail  exit   maintail  pid   reload  reread  shutdown  status  tail  version

就不一一解释啦

 

6. 使用web界面管理进程

http://xx.xx.xx.xx:11316/

 

 

 

几点注意的:

1. 有些程序是多进程的【master进程fork出m个子进程工作】,不大适合用supervisor管理,因为kill掉master进程后,子进程就会认init做父,再用supervisor启动就会有问题【其实我没试过】

2. supervisor如果挂了,它所管理的服务也不会死,会认init做父,如果这时候再启动supervisor,会发现被管理的服务起重了。 但是,如果被管理的服务需要监听端口。。那supervisor启动的时候,会出问题哦。【端口已经很占用,其实我没试过】

 

 

 

posted @ 2013-08-29 15:28 ~嘉言懿行~~我是煲仔饭~~ 阅读(...) 评论(...) 编辑 收藏