进程管理工具之Supervisor

首先Supervisor是一个进程监控及管理工具,与monit有点相似又不一样,同样可以监控进程,自动重启进程。

我的需求:对一些没有启动脚本的程序,多实例程序集中管理,并随时可以通过页面重启某个程序,比如搬瓦工的vps,elk,redis,java进程等等,都适用。好了,废话不多说,开始安装使用

1,安装supervisor(centos)

yum install python-setuptools
easy_install supervisor

测试是否安装成功:

echo_supervisord_conf

安装还是比较简单,就2条命令,安装完毕会多出2个命令supervisord,supervisorctl

supervisord : supervisor的服务器端部分,启动supervisor就是运行这个命令

supervisorctl:启动supervisor的命令行窗口。

2,创建配置文件

echo_supervisord_conf > /etc/supervisord.conf

执行之后会在/etc下生成配置文件supervisord.conf

3,修改配置文件

将[inet_http_server]的注释全部去掉,在末尾追加你要控制的进程,下面贴出我的精简配置

[unix_http_server]
file=/tmp/supervisor.sock   ; (the path to the socket file)

[inet_http_server]         ; inet (TCP) server disabled by default
port=192.168.216.236:8090        ; (ip_address:port specifier, *:port for all iface)
username=user              ; (default is no username (open server))
password=passwd               ; (default is no password (open server))

[supervisord]
logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.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=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.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)

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

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket

[program:ss_19999_server]
command = ssserver -c /etc/shadowsocks.json
autostart=true
autorestart=true

[program:ss_20001_server]
command = ssserver -c /etc/shadowsocks1.json
autostart=true
autorestart=true

[program:ss_20002_server]
command = ssserver -c /etc/shadowsocks2.json
autostart=true
autorestart=true

4,运行supervisord

编写启动脚本

vi /etc/init.d/supervisord
#!/bin/bash
#
# supervisord   This scripts turns supervisord on
#
# Author:       Mike McGrath <mmcgrath@redhat.com> (based off yumupdatesd)
#
# chkconfig:    - 95 04
#
# description:  supervisor is a process control utility.  It has a web based
#               xmlrpc interface as well as a few other nifty features.
# processname:  supervisord
# config: /etc/supervisord.conf
# pidfile: /var/run/supervisord.pid
#

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0

start() {
        echo -n $"Starting supervisord: "
        daemon supervisord
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/supervisord
}

stop() {
        echo -n $"Stopping supervisord: "
        killproc supervisord
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/supervisord
}

restart() {
        stop
        start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|force-reload|reload)
        restart
        ;;
  condrestart)
        [ -f /var/lock/subsys/supervisord ] && restart
        ;;
  status)
        status supervisord
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
        exit 1
esac

exit $RETVAL

/etc/init.d/supervisord start

PS:启动会报一些错误,不过不影响,完全可以忽略,检查supervisord管理的进程以及8090端口,如果启动则代表正常

或者运行命令:

supervisord    //启动supervisor

supervisorctl   //打开命令行

ctl中: help   //查看命令

ctl中: status  //查看状态

5,web管理

打开管理页面,可以看到进程启动时间,可以手动操作重启,log只记录报错信息,如果进程有问题会打印错误日志。

supervisor

此条目发表在linux分类目录。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注