设置 /etc/redis.conf
中 daemonize
为 yes
,确保守护进程开启,也就是在后台可以运行.(设置为yes后,启动时好像没有redis的启动界面,不知道为什么)
编写脚本
vim /etc/init.d/redis
复制下面代码到脚本中(注意要修改里面redis的安装路径,以 /usr/local/redis
路径为例)(这段代码就是redis根目录 /utils/redis_init_script
启动脚本的代码)
#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis
REDISPORT=6379
EXEC=/usr/local/redis/src/redis-server
CLIEXEC=/usr/local/redis/src/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis.conf"
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
;;
restart)
"$0" stop
sleep 3
"$0" start
;;
*)
echo "Please use start or stop or restart as first argument"
;;
esac
保存退出,设置权限
chmod 777 /etc/init.d/redis
启动redis
service redis start
启动redis
设置开机启动
chkconfig redis on
测试开机启动
reboot
开机完成测试redis是否打开
redis-cli ping
redis-cli ping
网友评论