一、新建、编写开机自启动脚本(redis-auto为新建的文件)
vi /etc/init.d/redis-auto
二、在文件redis-auto加入如下内容:(注意Redis具体的安装路径,你的可能不一样)
#!/bin/sh
#chkconfig: 2345 80 90
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
#redis basepath
BASE_PATH = /home/java/run/redis-3.2.9/bin
#redis port
REDIS_PORT=6379
#Redis servie
EXEC=${BASE_PATH}/redis-server
#redis client
CLIEXEC=${BASE_PATH}/redis-cli
#redis config file
CONF=${BASE_PATH}/redis.conf
#this file is auto generate
PIDFILE=/var/run/redis_${REDISPORT}.pid
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
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
三、保存退出
wq
四、设置文件redis-auto的权限,让Linux可以执行
chmod 755 redis-auto
五、启动Redis服务测试,此处启动用的是第二步设置的启动脚本
/etc/init.d/redis-auto start
如果看到Redis启动的小盒子就表示成功。
不过你可以进一步打开redis-cli客户端进行测试
六、设置开机自启动,即:
chkconfig redis-auto on
七、经过测试,Linux系统在重新启动时,Redis的数据会自动丢失,解决方案见:
Linux Redis 重启数据丢失解决方案,Linux重启后Redis数据丢失解决方案
http://www.linuxdiyf.com/linux/32076.html