在centos上安装最新版supervisord后台服务

#安装python的安装工具pip
wget http://mirrors.opencas.cn/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm
yum -y install python-pip

#安装supervisord
pip install supervisor

#测试安装是否成功
echo_supervisord_conf

#创建配置文件
mkdir -p /etc/supervisor
mkdir -m 755 -p /etc/supervisor/
echo_supervisord_conf > /etc/supervisord.conf
sed -i "s/\;\[include\]/\[include\]/" /etc/supervisord.conf
echo "files = /etc/supervisor/*.conf" >> /etc/supervisord.conf 

########################################################

#在centos 7.x下创建服务
cat > /usr/lib/systemd/system/supervisord.service << EOF
[Unit]
Description=Supervisor daemon

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

[Install]
WantedBy=multi-user.target
EOF
systemclt enable supervisord

########################################################

#在centos 6.x下创建服务

cat > /etc/init.d/supervisord << EOF
#!/bin/sh
#
# /etc/rc.d/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"


prefix="/usr/"
exec_prefix="${prefix}"
prog_bin="${exec_prefix}/bin/supervisord"
PIDFILE="/var/run/$prog.pid"


start()
{
        echo -n $"Starting $prog: "
        daemon $prog_bin --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
EOF

chmod +x /etc/init.d/supervisord
chkconfig --add supervisord
chkconfig supervisord --level 2345 on

########################################################

# 创建一个监听端口为6380、非后台服务的redis配置文件
cat > /etc/redis/redis-6380.conf << EOF
daemonize no
pidfile /var/run/redis_6380.pid
port 6380
logfile /var/log/redis_6380.log
dir /var/lib/redis/6380
EOF

#创建被监控应用的配置
cat > /etc/supervisor/redis-6380.conf << EOF
[program:redis-6380]
command=/usr/local/bin/redis-server /etc/redis/6380.conf
numprocs=1                    ; 启动几个进程
autostart=true                ; 随着supervisord的启动而启动
autorestart=true              ; 自动重启。。当然要选上了
startretries=10               ; 启动失败时的最多重试次数
exitcodes=0                   ; 正常退出代码(是说退出代码是这个时就不再重启了吗?待确定)
stopsignal=KILL               ; 用来杀死进程的信号
stopwaitsecs=10               ; 发送SIGKILL前的等待时间
redirect_stderr=true          ; 重定向stderr到stdout
EOF

#重启supervisord
supervisorctl reload

# 查看redis进程号
ps aux | grep redis | grep -v grep
# 输出为:root      4290  0.3  0.3 140836  7860 ?        Sl   23:40   0:00 /usr/local/bin/redis-server *:6380

# 杀死这个redis进程
kill  `ps aux | grep redis | grep -v grep | awk '{print $2}'`

# 查看redis新的进程号
ps aux | grep redis | grep -v grep
# 输出为:root      1040  0.1  0.3 140836  7860 ?        Sl   23:48   0:00 /usr/local/bin/redis-server *:6380

你可能感兴趣的:(linux)