Linux环境配置redis集群启动、停止脚本

1、创建redisc脚本

vim /etc/init.d/redisc

2、编辑脚本内容

#!/bin/sh
# chkconfig: 2345 80 90
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISNODE1=7001
REDISNODE2=7002
REDISNODE3=7003
REDISNODE4=7004
REDISNODE5=7005
REDISNODE6=7006

PASSWORD=123456

HOSTIP=172.25.41.229
 
EXEC=/usr/local/redis/redis-6.2.1/src/redis-server
CLIEXEC=/usr/local/redis/redis-6.2.1/src/redis-cli

PIDFILE=/usr/local/redis/redis_cluster/node${REDISNODE1}/nodes-${REDISNODE1}.pid

CONF1=/usr/local/redis/redis_cluster/node${REDISNODE1}/redis.conf
CONF2=/usr/local/redis/redis_cluster/node${REDISNODE2}/redis.conf
CONF3=/usr/local/redis/redis_cluster/node${REDISNODE3}/redis.conf
CONF4=/usr/local/redis/redis_cluster/node${REDISNODE4}/redis.conf
CONF5=/usr/local/redis/redis_cluster/node${REDISNODE5}/redis.conf
CONF6=/usr/local/redis/redis_cluster/node${REDISNODE6}/redis.conf

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis cluster server..."
                $EXEC $CONF1 &
                $EXEC $CONF2 &
                $EXEC $CONF3 &
                $EXEC $CONF4 &
                $EXEC $CONF5 &
                $EXEC $CONF6 &
                echo "启动成功..."
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -h $HOSTIP -p $REDISNODE1 -a $PASSWORD  shutdown
                $CLIEXEC -h $HOSTIP -p $REDISNODE2 -a $PASSWORD  shutdown
                $CLIEXEC -h $HOSTIP -p $REDISNODE3 -a $PASSWORD  shutdown
                $CLIEXEC -h $HOSTIP -p $REDISNODE4 -a $PASSWORD  shutdown
                $CLIEXEC -h $HOSTIP -p $REDISNODE5 -a $PASSWORD  shutdown
                $CLIEXEC -h $HOSTIP -p $REDISNODE6 -a $PASSWORD  shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis cluster to shutdown ..."
                    sleep 1
                done
                echo "Redis cluster stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

3、将redisc注册为应用服务

chkconfig --add redisc

4、通过以下两种命令进行启动和停止redis集群

service redisc start

service redisc stop

5、如果提示没有权限通过以下命令设置

chmod a+x /etc/init.d/redisc

6、设置开机自启动

chkconfig redisc on

posted @ 2023-11-16 23:15  Be_Your_Sun  阅读(686)  评论(0编辑  收藏  举报