专注,勤学,慎思。戒骄戒躁,谦虚谨慎

just do it

导航

Redis服务方式启动脚本


Redis没有默认的服务脚本,方便管理的情况下需要自定义一个服务脚本,对于实例的启动好说,对于关闭Redis进程,有两种方式
1,直接杀进程,kill -9的方式,过于简单粗暴
2,基于redis-cli shutdown的方式,需要数据密码(如果脑子没问题都会启用密码认证)
网上的两个版本,对于启动redis进程来说,是比较简单的,第一种是简单粗暴通过kill进程的方式,第二种是redis-cli shutdown但是是基于无密码的方式的,都不实用,白嫖是不可能白嫖的了。
于是就简单改写了一个基于密码认证的redis-cli shutdown方式,开启了密码认证的情况下,在stop或者restart的时候,输入一个密码参数,并且验证密码的有效性,然后基于redis-cli shutdown关闭/重启实例。

这里是Redis服务的模板,
1,基于当前实例的端口号判断服务状态,而不是pid文件,因为如果通过kill方式杀掉的进程,pid文件仍然会存在于文件中。
2,在stop或者restart的时候,需要对密码参数进行验证
3,在执行redis-cli shutdown的时候,如果密码出错,也需要进行验证,这里基于错误密码验证失败后的NOAUTH Authentication required.关键字进行判断

#!/bin/sh
# chkconfig: 2345 10 90 
# description: Start and Stop redis
port=db_port
basedir=base_dir
datadir=data_dir
conf=data_dir/redis.conf
case "$1" in
  start)
    process=`netstat -ntl|grep $port | wc -l`
    if [ $process -eq 0 ]
    then
        cd ${datadir}
        $basedir/bin/redis-server ./redis.conf&
        if [ $? -eq 0 ]; then
            echo "Starting Redis server sucefully"
        else
            echo "Starting Redis server fail,refer to the log information for details"
        fi
    else
        echo "Port "$port is exists, process is already running""
    fi
    ;;
  stop)
    if [ ! $2 ];then
        echo "Redis password is required"
        exit -1
    fi
    process=`lsof -i:$port -t`
    if [ $process ]; then
        echo "Stopping ..."
        #kill -9 $process
        #$basedir/bin/redis-cli -p $port -a $2  shutdown 
        msg=`$basedir/bin/redis-cli -a $2 -p $port shutdown`
        result=$(echo $msg | grep "Authentication")
        if [[ "$result" = "" ]]; then
            while [ -x /proc/${process} ]
            do
                echo "Waiting for Redis to shutdown ..."
                sleep 1
            done
            echo "Redis stopped"
        else
            echo "Authentication failed,please check redis password" 
            exit -1
        fi
    else
        echo "Process is not running"
    fi
    ;;
  restart)
    process=`lsof -i:$port -t`
    if [ $process ]; then
        "$0" stop $2
        if [ $? -eq 0 ]; then
            "$0" start
        fi
    else
        "$0" start
    fi
    ;;
  *)
    echo "Please use start or stop or restart as first argument"
    ;;
esac

安装过程中替换相关的变量

#!/bin/bash
#sh redis_install.sh /usr/local/redis_install   /usr/local/redis_instance/redis8000/  redis-5.0.4  8000 192.168.0.66

set -e
if [ $# -lt 4 ]; then
    echo "$(basename $0): Missing script argument"
    echo "$(installdir $0) [installfilename] [port] "
    exit 9
fi

PotInUse=`netstat -anp |  awk '{print $4}'  | grep $4 | wc -l`
if [ $PotInUse -gt 0 ];then
    echo "ERROR" $4 "Port is used by another process!"
    exit 9
fi

basedir=${1%/}
installdir=${2%/}
installfilename=${3%/}
port=$4
bind_ip=$5

cd $basedir
echo "Unzip installation package "
tar -zxvf $installfilename.tar.gz  >/dev/null 2>&1 &
sleep 1s
mkdir -p $installfilename
cd $installfilename
mkdir -p $installdir
echo "Compile installation package"
make PREFIX=$basedir install >/dev/null 2>&1 &
sleep 1s 
cp $basedir/redis.conf $installdir

sed -i "s/instance_port/$port/g"  $installdir/redis.conf
sed -i "s/bind_ip/$bind_ip/g"  $installdir/redis.conf


cd $basedir

cp -f redis_6379  /etc/init.d/redis_$port
sed -i "s/db_port/$port/g" /etc/init.d/redis_$port
sed -i "s/base_dir/${basedir//\//\\/}/g" /etc/init.d/redis_$port
sed -i "s/data_dir/${installdir//\//\\/}/g" /etc/init.d/redis_$port

chmod +x /etc/init.d/redis_$port
sleep 1s
echo "Start redis service"
cd $installdir
$basedir/bin/redis-server redis.conf >/dev/null 2>&1 &
if [ $? -eq 0 ]; then
    echo "Starting Redis server sucefully"
else
    echo "Starting Redis server fail,refer to the log information for details"
fi

简单基于服务模式启动,关闭,重启redis服务,以及密码正确性验证

 

posted on 2020-12-10 21:48  MSSQL123  阅读(354)  评论(0编辑  收藏  举报