centos利用supervisord实现守护进程,保证后台常驻进程服务的高可用

86201746 2019-06-25

安装supervisor

#第一种
yum install python-setuptools
easy_install pip
pip install supervisor

#第二种
yum install python-setuptools
easy_install supervisor

#第三种
wget https://pypi.python.org/packages/source/s/supervisor/supervisor-3.1.3.tar.gz
tar zxvf supervisor-3.1.3.tar.gz
cd supervisor
python setup.py install

#第四种
yum install -y epel-release
yum install -y supervisor

配置

cd /etc/
mkdir supervisord.d
echo_supervisord_conf > supervisord.conf
vim /etc/supervisord.conf
#加入以下配置信息
[include]
files = /etc/supervisord.d/*.conf

开机启动服务文件

#!/bin/sh
#
# /etc/init.d/supervisord
#
# Supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord
 
# Source init functions
. /etc/rc.d/init.d/functions
 
prog="supervisord"

prog_bin="/usr/bin/supervisord"
PIDFILE="/var/run/$prog.pid"
 
start()
{
       echo -n $"Starting $prog: "
       ###注意下面这一行一定得有-c /etc/supervisord.conf   不然修改了配置文件根本不生效!
       daemon $prog_bin -c /etc/supervisord.conf --pidfile $PIDFILE
       [ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog startup"
       echo
}
 
stop()
{
       echo -n $"Shutting down $prog: "
       [ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown"
       echo
}
 
case "$1" in
 
 start)
   start
 ;;
 
 stop)
   stop
 ;;
 
 status)
       status $prog
 ;;
 
 restart)
   stop
   start
 ;;
 
 *)
   echo "Usage: $0 {start|stop|restart|status}"
 ;;
 
esac
chmod +x /etc/init.d/supervisord
chkconfig --add supervisord
chkconfig supervisord on
service supervisord start

开启web查看

# 如果想通过web查看管理的进程,加入以下代码,监听9001,用户user,密码123
[inet_http_server] 
port=9001
username=user      
password=123

创建一个任务

#在重启之前随便创建一个挂在后台的命令
vim /etc/supervisord.d/tail.conf

[program:task1]                                                                            
command=tail -f  /etc/supervisord.conf   ;常驻后台的命令
autostart=true                           ;是否随supervisor启动
autorestart=true                         ;是否在挂了之后重启,意外关闭后会重启,比如kill掉!
startretries=3                           ;启动尝试次数
stderr_logfile=/tmp/task1.err.log        ;标准输出的位置
stdout_logfile=/tmp/task1.out.log        ;标准错误输出的位置

查看服务是否启动

##查看一下是否监听
lsof -i:9001
COMMAND    PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
superviso 7782 root    4u  IPv4 74522612      0t0  TCP *:etlservicemgr (LISTEN)

web方式查看

http://ip:9001
#输入你上面设置的账号密码,管理台上可以启动和停止服务

相关推荐

Sophisticated / 0评论 2019-06-29