設(shè)置 /etc/redis.conf
中 daemonize
為 yes
,確保守護(hù)進(jìn)程開啟,也就是在后臺可以運(yùn)行.(設(shè)置為yes后,啟動時好像沒有redis的啟動界面,不知道為什么)
編寫腳本
vim /etc/init.d/redis
復(fù)制下面代碼到腳本中(注意要修改里面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
保存退出,設(shè)置權(quán)限
chmod 777 /etc/init.d/redis
啟動redis
service redis start
設(shè)置開機(jī)啟動
chkconfig redis on
測試開機(jī)啟動
reboot
開機(jī)完成測試redis是否打開
redis-cli ping