Redis的启动/停止/重启脚本

1、start.sh

#!/bin/sh

if [ $# -ne 1 ]; then
	echo "usage: $0 port"
	exit -1;
fi

PORT=$1

#您可以在此处修改redis-server和conf的所在路径
REDIS_SERVER="./redis-server"
REDIS_CLI="./redis-cli"
REDIS_CONF="./conf/redis_$PORT.conf"

if [ ! -e $REDIS_SERVER ]; then
	echo "$REDIS_SERVER does not exist!"
	exit -1
fi

if [ ! -e $REDIS_CONF ]; then 
	echo "$REDIS_CONF does not exist!"
	exit -1
fi

cmd="ps -ef | grep ${REDIS_SERVER} | grep -v grep | grep -v vim | grep -v defunct | grep '$PORT' | awk '{ print \$2 }'"
#echo $cmd

PID=$(eval ${cmd})
#echo $PID

if [ $PID"e" != "e" ]; then
	echo "redis-server(port:$PORT) is running, can't start"
	exit -1
else
	$REDIS_SERVER $REDIS_CONF &
fi


startfail=1
for i in `seq 0 180`
do
	PID=$(eval ${cmd})
	#echo $PID
	if [ $PID"e" != "e" ]; then
		${REDIS_SERVER} --version
		echo "Redis server(port:$PORT) is stared..."
		startfail=0
		break
	fi
	sleep 1
done

exit $startfail

2、stop.sh

#!/bin/sh

if [ $# -ne 1 ]; then
	echo "usage: $0 port"
	exit -1;
fi

PORT=$1

#您可以在此处修改redis-server的所在路径
REDIS_SERVER="./redis-server"

cmd="ps -ef | grep ${REDIS_SERVER} | grep -v grep | grep -v vim | grep -v defunct | grep ${PORT} | awk '{ print \$2 }'"
#echo $cmd

PID=$(eval ${cmd})

if [ ${PID}"e" = "e" ]; then
	echo "redis-server(port:$PORT) is not started"
	exit -1
else
	kill $PID
fi

stopfail=1
for i in `seq 0 30`
do
	PID=$(eval ${cmd})
	#echo $PID
	if [ ${PID}"e" != "e" ]; then
		echo "redis-server(port:$PORT) is still running, waiting to stop[${i}]..."
	else
		echo "redis-server(port:$PORT) is stoped"
		stopfail=0
	   break
   	fi
	sleep 1
done

exit $stopfail

3、restart.sh

#!/bin/sh

if [ $# -ne 1 ]; then
	echo "usage: $0 port"
	exit -1
fi

PORT=$1

./stop.sh $PORT
./start.sh $PORT

 

你可能感兴趣的:(Linux)