shell运维Redis
shell脚本实现Redis启停状态查询
单机下的Redis脚本处理
#!/bin/bash
#Redis启停脚本
#./opRedis.sh start 启动 stop 停止 status 进程查看
#Redis安装目录
REDIS_PATH=/opt/redis/
#Redis实例实际个数
REDIS_STARTING_NUM=`ps -ef|grep redis-server|grep -v grep|wc -l`
#Redis启动个数
REDIS_NORMAL_NUM=1
#进程pid
REDIS_PID=`ps -ef|grep redis-server|grep -v grep|awk '{print $2}'`
#检查输入参数
if [ "$1" = "" ];
then
echo -e "\033[0;31m 操作参数输入有误 \033[0m \033[0;34m {start|stop|status} \033[0m"
exit 1
fi
#检查参数个数
if [ $# -gt 1 ];
then
echo -e "\033[0;31m 操作参数个数[$#]有误,应为1 \033[0m"
exit 1
fi
function start()
{
echo "Starting..."
cd $REDIS_PATH
./bin/redis-server ./redis.conf
if [ $? -eq 0 ];
then
REDIS_PID=`ps -ef|grep redis-server|grep -v grep|awk '{print $2}'`
echo "Start succ,pid[$REDIS_PID]"
else
echo "Start fail"
fi
}
function stop()
{
if [ $REDIS_STARTING_NUM -eq 0 ];
then
echo -e "\033[0;31m 当前Redis启动实例数[$REDIS_STARTING_NUM],无需停止 \033[0m"
return 0
fi
kill -9 $REDIS_PID
if [ $? -eq 0 ];
then
echo "Stop succ,pid[$REDIS_PID]"
else
echo "Stop fail,pid[$REDIS_PID]"
fi
}
function status()
{
if [ $REDIS_STARTING_NUM -eq 0 ];
then
echo -e "\033[0;31m 当前Redis启动实例数[$REDIS_STARTING_NUM],Redis未启动 \033[0m"
return 0
else
echo -e "\033[0;32m 当前Redis启动实例数[$REDIS_STARTING_NUM],PID[$REDIS_PID] \033[0m"
cd $REDIS_PATH
#./bin/redis-cli -r 1 ping
PONG_MSG=`./bin/redis-cli -r 1 ping`
if [ $PONG_MSG = "PONG" ];
then
echo -e "\033[0;32m Redis执行PING操作检查正常[$PONG_MSG] \033[0m"
else
echo -e "\033[0;31m Redis执行PING操作检查异常[$PONG_MSG],请检查 \033[0m"
return 2
fi
fi
}
case $1 in
start)
start;;
stop)
stop;;
status)
status;;
*)
echo -e "\033[0;31m 操作参数输入有误 \033[0m \033[0;34m {start|stop|status} \033[0m";;
esac