第一步 修改redis为后台启动

vim /usr/redis/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

第二步 将redis配置为服务形式

vim /etc/init.d/redis
# chkconfig: 2345 10 90
# description: Start and Stop redis

PATH=/usr/local/bin:/sbin:/usr/bin:/bin

REDISPORT=6379 #实际环境而定
EXEC=/usr/redis/redis-server #实际环境而定
REDIS_CLI=/usr/redis/redis-cli #实际环境而定

PIDFILE=/var/run/redis.pid
CONF="/usr/redis/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
                if [ "$?"="0" ]
                then
                        echo "Redis is running..."
                fi
                ;;
        stop)
                if [ ! -f $PIDFILE ]
                then
                        echo "$PIDFILE exists, process is not running."
                else
                        PID=$(cat $PIDFILE)
                        echo "Stopping..."
                        $REDIS_CLI -p $REDISPORT 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

第三步 配置开机启动

chmod 755 /etc/init.d/redis
chkconfig redis on    # 开启服务自启动

service redis start  #启动redis
service redis stop   #关闭redis

第四步 检测是否成功

init 6  #重启
ps -fe | grep redis #查看redis进程是否存在

2017-08-04