脚本功能:
实现redis单机多实例情况下的正常启动、关闭、重启单个redis实例。完成系统标准服务的以下常用功能: start|stop|status|restart
注:redis程序代码屏蔽了HUP信号,不支持在线重载配置文件,故去掉reload功能。
本脚本优化了redis停止和重启逻辑,解决原redis脚本关闭时会造成数据丢失问题。
脚本名称:
redis #在多实例里可以按实例端口,如:redis-6001命名,以区分不同实例
脚本用法:
1.在/etc/rc.d/init.d/目录下新建redis文件,将脚本内容拷贝进去
2. chkconfig --add redis #注册服务
3. chkconfig --level 345 redis on #指定服务在3、4、5级别运行
4.本人redis程序安装在/usr/local/redis目录下,配置为/usr/local/redis/bin/redis.conf,如安装在其他目录,请自行修改
脚本参数:
redis -p [port] [start|stop|status|restart]
参数说明:
-p [port] : 指定redis实例的端口,用于多实例的服务器
start:启动指定端口的Redis服务
stop:停止指定端口的Redis服务
status:进程状态
restart:先关闭Redis服务,再启动Redis服务
注:不指定端口时,脚本默认指定启动6379端口的redis
用法实例:
service redis -p 6381 start #启动6381端口实例的redis
/etc/init.d/redis start #默认启动6379端口实例的redis
脚本内容:
#!/bin/bash
#chkconfig: 2345 55 25
#description: Starts,stops and restart the redis-server
#Ver:1.1
#Write by ND chengh(200808)
#usage: ./script_name -p [port] {start|stop|status|restart}
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check networking is up.
[ "$NETWORKING" = "no" ] && exit 0
RETVAL=0
REDIS_PORT=6379
PID=
if [ "$1" = "-p" ]; then
REDIS_PORT=$2
shift 2
fi
REDIS_DIR="/usr/local/redis"
REDIS="${REDIS_DIR}/bin/redis-server"
PROG=$(basename $REDIS)
CONF="${REDIS_DIR}/bin/redis-${REDIS_PORT}.conf"
if [ ! -f $CONF ]; then
if [ -f "${REDIS_DIR}/bin/redis.conf" ];then
CONF="${REDIS_DIR}/bin/redis.conf"
else
echo -n $"$CONF not exist.";warning;echo
exit 1
fi
fi
PID_FILE=`grep "pidfile" ${CONF}|cut -d ' ' -f2`
PID_FILE=${PID_FILE:=/var/run/redis.pid}
LOCKFILE="/var/lock/subsys/redis-${REDIS_PORT}"
if [ ! -x $REDIS ]; then
echo -n $"$REDIS not exist.";warning;echo
exit 0
fi
start() {
echo -n $"Starting $PROG: "
$REDIS $CONF
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
success;echo;touch $LOCKFILE
else
failure;echo
fi
return $RETVAL
}
stop() {
echo -n $"Stopping $PROG: "
if [ -f $PID_FILE ] ;then
read PID < "$PID_FILE"
else
failure;echo;
echo -n $"$PID_FILE not found.";failure;echo
return 1;
fi
if checkpid $PID; then
kill -TERM $PID >/dev/null 2>&1
RETVAL=$?
if [ $RETVAL -eq 0 ] ;then
success;echo
echo -n "Waiting for Redis to shutdown .."
while checkpid $PID;do
echo -n "."
sleep 1;
done
success;echo;rm -f $LOCKFILE
else
failure;echo
fi
else
echo -n $"Redis is dead and $PID_FILE exists.";failure;echo
RETVAL=7
fi
return $RETVAL
}
restart() {
stop
start
}
rhstatus() {
status -p ${PID_FILE} $PROG
}
hid_status() {
rhstatus >/dev/null 2>&1
}
case "$1" in
start)
hid_status && exit 0
start
;;
stop)
rhstatus || exit 0
stop
;;
restart)
restart
;;
status)
rhstatus
RETVAL=$?
;;
*)
echo $"Usage: $0 -p [port] {start|stop|status|restart}"
RETVAL=1
esac
exit $RETVAL
废话少说,直接来步骤:
1、设置redis.conf中daemonize为yes,确保守护进程开启。
2、编写开机自启动脚本
vi /etc/init.d/redis
脚本内容如下:
# chkconfig: 2345 10 90 # description: Start and Stop redis PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 EXEC=/usr/redisbin/redis-server REDIS_CLI=/usr/redisbin/redis-cli PIDFILE=/var/run/redis.pid CONF="/use/redisbin/redis.conf" AUTH="1234" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed." else echo "Starting Redis server..." $EXEC $CONF fi if [ "$?"="0" ] then echo "Redis is running..." fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE exists, process is not running." else PID=$(cat $PIDFILE) echo "Stopping..." $REDIS_CLI -p $REDISPORT SHUTDOWN sleep 2 while [ -x $PIDFILE ] do echo "Waiting for Redis to shutdown..." sleep 1 done echo "Redis stopped" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1 esac
3、写完后保存退出VI
4、设置权限
chmod 755 redis
5、启动测试
/etc/init.d/redis start
启动成功会提示如下信息:
Starting Redis server... Redis is running...
使用redis-cli测试:
[root@rk ~]# /usr/redisbin/redis-cli 127.0.0.1:6379> set foo bar OK 127.0.0.1:6379> get foo "bar" 127.0.0.1:6379> exit
6、设置开机自启动
chkconfig redis on
7、关机重启测试
reboot
然后在用redis-cli测试即可。