一、/etc/init.d/ 设置开机自启动

1、确保redis.conf中daemonize为yes,确保守护进程开启

2、编写开机自启动脚本

vi /etc/init.d/redis_6379

脚本内容如下:  #脚本模板可通过find / -name redis_init_script查找# #其他自启动脚本不同,方法一样#

# chkconfig: 2345 10 90  
# description: Start and Stop redis   
  
PATH=/usr/local/bin:/sbin:/usr/bin:/bin   

 REDISPORT=6376
 EXEC=/usr/local/redis/src/redis-server
 REDIS_CLI=/usr/local/redis/src/redis-cli

 PIDFILE=/data/redis/redis_6376.pid
 CONF=”/data/redis/etc/redis_6376.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    
                        sleep 2  
                       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

然后保存

3、设置权限

chmod 755 redis

4、启动测试

/etc/init.d/redis start

启动成功会提示如下信息:

Starting Redis server...
Redis is running...

5、设置开机自启动

chkconfig redis on

6、关机重启测试

二、/etc/rc.local 设置开机自启动 #与/etc/init.d/的区别可以自行度娘

vim /etc/rc.local

然后再里面添加

redis-server /data/redis/etc/redis_6376.conf