启停 Redis 集群脚本

因为redis集群在停止和启动上都存在很大的繁琐性,所以就写了一个简单的启停redis集群的脚本

关闭脚本
stop-redis.sh
#!/bin/bash
PORT=($1 $2)

for port in ${PORT[@]}
do
PID=$(netstat -ntulp | grep :$port | awk '{print $7}' | awk -F"/" '{print $1}')

if [ $? -eq 0 ]; then
    echo "process id: $PID"
else
    echo "process process not exist"
    exit
fi

kill -9 ${PID}

if [ $? -eq 0 ]; then
    echo "kill $port success"
else
    echo "kill $port fail"
fi
done
stop-redis-cluster.sh
#!/bin/bash
ssh yangqi@xiaoer > /dev/null 2>&1 << eeooff stop-redis.sh $1 $2 exit eeooff
echo xiaoer:redis-cluster done!

ssh yangqi@yangqi1 > /dev/null 2>&1 << eeooff stop-redis.sh $3 $4 exit eeooff
echo yangqi1:redis-cluster done!

ssh yangqi@yangqi2 > /dev/null 2>&1 << eeooff stop-redis.sh $5 $6 exit eeooff
echo yangqi2:redis-cluster done!

在进行运行之前,一定要注意自己的主机的hostname,和我的可能不一样,需要进行调整,之后将stop-redis.sh脚本一定要放在每台redis节点的主机的用户目录的bin目录下,并且授予脚本可执行权限。

# 授予可执行权限
[yangqi@xiaoer bin]$ chmod 764 stop-redis.sh
[yangqi@yangqi1 bin]$ chmod 764 stop-redis.sh
[yangqi@yangqi2 bin]$ chmod 764 stop-redis.sh

[yangqi@xiaoer bin]$ chmod 764 stop-redis-cluster.sh
运行脚本

运行stop-redis-cluster.sh脚本时,可以传入参数:

# redis 集群节点和端口号如下:
xiaoer -> 7000 7001
yangqi1 -> 7002 7003
yangqi2 -> 7004 7005

# 启动脚本命令:注意,一定要按照 stop-redis-cluster.sh 脚本中远程连接主机的顺序输入端口号,避免造成主机和端口号不匹配,导致关闭失败
[yangqi@xiaoer bin]$ stop-redis-cluster.sh 7000 7001 7002 7003 7004 7005
启动脚本
start-redis-cluster.sh
#!/bin/bash
ssh yangqi@xiaoer > /dev/null 2>&1 << eeooff cd /opt/apps/redis-cluster ./redis-server ./7000/redis.conf ./redis-server ./7001/redis.conf exit eeooff
echo xiaoer:redis-cluster start done!

ssh yangqi@yangqi1 > /dev/null 2>&1 << eeooff cd /opt/apps/redis-cluster ./redis-server ./7002/redis.conf ./redis-server ./7003/redis.conf exit eeooff
echo yangqi1:redis-cluster start done!

ssh yangqi@yangqi2 > /dev/null 2>&1 << eeooff cd /opt/apps/redis-cluster ./redis-server ./7004/redis.conf ./redis-server ./7005/redis.conf exit eeooff
echo yangqi2:redis-cluster start done!

授予可执行权限:

[yangqi@xiaoer bin]$ chmod 764 start-redis-cluster.sh