1. 程式人生 > >在centos上安裝最新版supervisord後臺服務

在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