redis 设置开机自启

配置init脚本

创建init脚本存于/etc/init.d/下,并命名为redis

# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/etc/redis.conf"
case "1" in
start)
if [ -fPIDFILE ]
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 does not exist, process is not running"
else
PID=(catPIDFILE)
echo "Stopping ..."
REDIS_CLI -pREDISPORT SHUTDOWN
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

修改redis.conf

修改redis.conf,将 daemonize no 改为 daemonize yes

设定开机启动服务

chkconfig redis on

启动&停止redis

#启动
service redis start
#停止
redis-cli
>shutdown
not connected>exit

测试redis

redis-cli
redis 127.0.0.1:6379> set foo 123
OK
redis 127.0.0.1:6379> get foo
"123"
redis 127.0.0.1:6379> exit

发表评论