#!/bin/bash
# 2024-12

#获取脚本根目录，并设为只读
readonly INITDIR=$(cd $(dirname $0); dirname "$PWD")

STAMP=`date +"%Y%m%d%H%M%S"`

if [ ! -d "/opt/redis-6.0.5/" ]; then
  echo "检测无/opt/redis-6.0.5/目录"
else
  pkill -f -9 redis
  mv /opt/redis-6.0.5/ /opt/redis-6.0.5_$STAMP
  echo "检测已存在/opt/redis-6.0.5/目录，并已重命名备份"
fi

tar -zxf $INITDIR/file/redis-6.0.5.tar.gz -C /opt/
source $INITDIR/conf/init.conf
sed -i "s#zwlbsweb#$WEB_IP#g" /opt/redis-6.0.5/*.conf  /opt/redis-6.0.5/conf/*
echo "redis安装完成。"

cat >  /opt/redis-6.0.5/run.sh  << EOF
#!/bin/bash

task_start()
{
  task_stop >/dev/null 2>&1
  sleep 2
  /opt/redis-6.0.5/src/redis-server /opt/redis-6.0.5/conf/redis-6379.conf
  /opt/redis-6.0.5/src/redis-sentinel  /opt/redis-6.0.5/conf/sentinel-26379.conf --protected-mode no
  /opt/redis-6.0.5/src/redis-server /opt/redis-6.0.5/conf/redis-6378.conf
  /opt/redis-6.0.5/src/redis-sentinel  /opt/redis-6.0.5/conf/sentinel-26378.conf --protected-mode no
}

task_stop()
{
  ps -ef | grep -v grep | grep "/opt/redis-6.0.5/src/.*:" | awk '{print \$2}' | xargs kill -9 
}

task_status()
{
  ps -ef | grep -v grep | grep "/opt/redis-6.0.5/src/.*\*:*"
  netstat -lntp | grep -E ':6379 |:6378 |:26379 |:26378'
}

case "\$1" in
  start)
    task_start
    ;;
  status)
    task_status
    ;;
  stop)
    task_stop
    ;;
  restart)
    task_start
    ;;
  *)
    echo "请输入正确参数：start | stop | restart | status"
    ;;
esac
EOF

chmod +x /opt/redis-6.0.5/run.sh

sed -i '\/opt\/redis-6.0.5\/run\.sh/d' /etc/rc.d/rc.local
echo '/opt/redis-6.0.5/run.sh start' >> /etc/rc.d/rc.local

/opt/redis-6.0.5/run.sh start



