啓停 Redis 集羣腳本

啓停 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章