修改redis.conf,打开后台运行选项:
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
编写脚本,vim /etc/init.d/redis:
1 # chkconfig: 2345 10 90 2 # description: Start and Stop redis 3 4 PATH=/usr/local/bin:/sbin:/usr/bin:/bin 5 6 REDISPORT=6379 #实际环境而定 7 EXEC=/usr/local/redis/src/redis-server #实际环境而定 8 REDIS_CLI=/usr/local/redis/src/redis-cli #实际环境而定 9 10 PIDFILE=/var/run/redis.pid 11 CONF="/usr/local/redis/redis.conf" #实际环境而定 12 13 case "$1" in 14 start) 15 if [ -f $PIDFILE ] 16 then 17 echo "$PIDFILE exists, process is already running or crashed." 18 else 19 echo "Starting Redis server..." 20 $EXEC $CONF 21 fi 22 if [ "$?"="0" ] 23 then 24 echo "Redis is running..." 25 fi 26 ;; 27 stop) 28 if [ ! -f $PIDFILE ] 29 then 30 echo "$PIDFILE exists, process is not running." 31 else 32 PID=$(cat $PIDFILE) 33 echo "Stopping..." 34 $REDIS_CLI -p $REDISPORT SHUTDOWN 35 while [ -x $PIDFILE ] 36 do 37 echo "Waiting for Redis to shutdown..." 38 sleep 1 39 done 40 echo "Redis stopped" 41 fi 42 ;; 43 restart|force-reload) 44 ${0} stop 45 ${0} start 46 ;; 47 *) 48 echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 49 exit 1 50 esac
执行权限:
chmod +x /etc/init.d/redis
开机自启动:
# 尝试启动或停止redis
service redis start
service redis stop
# 开启服务自启动
chkconfig redis on