Centos 6.8版本redis开机自启动配置

项目中经常用到redis,每次服务器重启后手工启动redis太繁琐,所以就想办法将redis服务加入开机自启。

步骤如下:

注意:下面方法只在centos 6.8版本系统试过,centos 7.x是否有效,尚未测试。

1、配置下redis的配置文件,添加一下daemonize yes ,确保redis以守护进程运行。

2、在/etc/init.d 目录下编写redis的启动文件,

vim /etc/init.d/redis

代码如下,其中

CONF=/usr/local/redis/etc/redis.conf    
AUTH="1234"  

CONF为redis配置文件存放地址,AUTH为redis密码,如果没有密码,则省略这行

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# chkconfig:   2345 90 10

# description:  Redis is a persistent key-value database

REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis.pid
CONF=/usr/local/redis/etc/redis.conf
AUTH="1234" 

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 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|force-reload)   
                ${0} stop   
                ${0} start   
                ;;  
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

3、保存后给该文件755权限

chmod 755 redis

4、设置开机自启动,命令如下:

chkconfig redis on

5、关机重启测试下,

reboot

使用 命令 查看redis进程:ps aux | grep redis

如果有redis进程在运行,说明启动成功了。

你可能感兴趣的:(redis,redis,linux)