Linux Centos7.3 开机启动 Redis 服务

1.正确安装 redis  (网上找找)

2.设置redis.conf中daemonize为yes,确保守护进程开启。

3.编辑redis 开机启动redis脚本

vi /etc/init.d/redis
脚本内容

#!/bin/sh
# chkconfig: 2345 80 90  
# description: Start and Stop redis   
  
#PATH=/usr/local/bin:/sbin:/usr/bin:/bin   
REDISPORT=6379  
EXEC=/usr/work/redis/start/redis-server   
REDIS_CLI=/usr/work/redis/start/redis-cli   
 
PIDFILE=/var/run/redis_6379.pid 
CONF="/usr/work/redis/start/redis.conf"  
AUTH="123456"  

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 -a $AUTH -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

Linux Centos7.3 开机启动 Redis 服务_第1张图片

根据实际情况修改,然后保存退出 

:wq

4.注册系统服务

chkconfig --add redis
5.测试redis  服务

service redis start
service redis stop

6.设置开机自启动

chkconfig redis on
7.关机重启测试

reboot


附:

可执行 ps -ef | grep redis 查询redis是否运行起来

可执行 chkconfig --list 查询redis服务在不同运行级的状态,必须在 2345 为 on, 脚本的第二行代码已经设置了


(原脚本在start节点下)

$EXEC $CONF

修改为:

$EXEC $CONF &

注:&会使服务在后台运行,不添加此符号redis会显示在前台,并影响其它服务的启动。

$REDIS_CLI -p $REDISPORT  SHUTDOWN
修改为

$REDIS_CLI -a $AUTH -p $REDISPORT  SHUTDOWN  
注:否则执行 service redis stop 的时候会出现   (error) NOAUTH Authentication required.的错误


本文参考以下文章,做个笔记,以备不时之需

http://www.cnblogs.com/silent2012/p/4157728.html

https://itbilu.com/linux/management/NkbXG9kol.html#copy-init-script

http://www.cnblogs.com/panjun-Donet/archive/2010/08/10/1796873.html

你可能感兴趣的:(Redis)