Redis 多实例脚本

2018年3月13日
#!/usr/bin/env bash
#
# redis start up the redis server daemon
#
# chkconfig: name  on

# description: redis service  in  /shell/redis

# Usage : ./Redis {start|stop|restart|kill|connect} port

# processname: redis-server

# config: /etc/redis.conf
# By zhongzuzhu

PATH=/usr/local/bin:/sbin:/usr/bin:/bin

REDIS_PORT=$2
REDIS_PWD=''
EXEC=/usr/bin/redis-server
REDIS_CLI=/usr/bin/redis-cli
HOST="127.0.0.1"

if [ $REDIS_PORT = "6379" ]; then
REDIS_CONFIG="/etc/redis.conf"
PIDFILE="/var/run/redis/redis.pid"
else
REDIS_CONFIG="/etc/redis-${REDIS_PORT}.conf"
PIDFILE="/var/run/redis/redis-${REDIS_PORT}.pid"
fi

SHUTDOWN="/usr/bin/redis-shutdown"

# Description of each port function

case $REDIS_PORT in
6379)
export project='Redis'
;;
6380)
export project='REDIS 6380 PROJECT IS'
;;
6687)
export project='REDIS 6687 PROJECT IS'
;;
6688)
export project='REDIS 6688 PROJECT IS'
;;
*)
;;
esac

echo $project

#make sure some dir exist
#这里是可不要的 根据自己的需求选择

if [ ! -d /var/lib/redis/${REDIS_PORT} ] ;then

mkdir -p /var/lib/redis/${REDIS_PORT}
chown -R redis:redis  /var/lib/redis/${REDIS_PORT}

touch /var/log/redis/redis-${REDIS_PORT}.log
chown -R redis:redis  /var/log/redis/redis-${REDIS_PORT}.log

fi

function_start_Redis()
{
#printf "Starting Redis... "
#/usr/bin/redis-server   $REDIS_CONFIG 2>&1 > /dev/null &

if [ -f $PIDFILE ]

then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC  $REDIS_CONFIG 2>&1 > /dev/null &
retval=$?

fi

if [ "$retval"="0" ]
then
echo -e  "Redis is running..."
echo -e  "\033[32m OK \033[0m"
else
echo -e "Redis start \033[31m Failed  \033[0m"
fi

}

function_stop_Redis()
{
#printf "Stoping Redis... \n"
#redis-cli -h $HOST -p $REDIS_PORT shutdown

if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -h $HOST  -p $REDIS_PORT shutdown
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
}

function_restart_Redis()
{
printf "Restarting Redis...\n "
function_stop_Redis
sleep 3;
function_start_Redis
}

function_kill_Redis()
{
kill -9 $(ps -ef | grep 'redis-server' | grep ${REDIS_PORT} | awk '{printf $2}')
#kill -9 $(ps -ef | grep 'redis-server' | grep ${REDIS_PORT} | awk '{printf $2}')
}

function_status_Redis()
{
PID=`ps -ef | grep 'redis-server' | grep ${REDIS_PORT} | awk '{printf $2}'`
#echo $PID
if [ ! -f $PIDFILE ]
then
echo "This Port redis  does not exist, process is not running"
else
printf "Redis is running ... \n"
printf "pid is `cat $PIDFILE` ... \n"
fi
}

function_enter_Redis()
{
printf "Entering Redis... \n"
redis-cli -h $HOST -p $REDIS_PORT
}

if [ "$1" = "start" ]; then
function_start_Redis
elif [ "$1" = "stop" ]; then
function_stop_Redis
elif [ "$1" = "restart" ]; then
function_restart_Redis
elif [ "$1" = "connect" ]; then
function_enter_Redis
elif [ "$1" = "status" ]; then
function_status_Redis
elif [ "$1" = "kill" ]; then
function_kill_Redis
else
printf "Usage: $0 {start|stop|restart|kill|connect} port \n"
fi

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注