美文网首页
设置redis服务开机自启动

设置redis服务开机自启动

作者: 码农工号9527 | 来源:发表于2020-07-03 16:52 被阅读0次

    1、在写脚本前设置下redis允许在后台启动,修改redis.conf文件,把daemonize no改成daemonize yes


    1.png

    2、开始编写脚本

    vim /etc/init.d/redis
    

    我的redis安装目录为/usr/local/redis,redis配置文件为/usr/local/redis/redis.conf,redis的pid文件为/var/run/redis_6379.pid

    #!/bin/bash
    #chkconfig: 22345 10 90
    #description: Start and Stop redis
    
    REDISPORT=6379
    EXEC=/usr/local/redis/src/redis-server
    CLIEXEC=/usr/local/redis/src/redis-cli
    
    PIDFILE=/var/run/redis_6379.pid
    CONF="/usr/local/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
            ;;
        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)
            "$0" stop
            sleep 3
            "$0" start
            ;;
        *)
            echo "Please use start or stop or restart as first argument"
            ;;
    esac
    

    编写完后还需要赋予它可执行权限,否则报如下错误:

    [root@localhost redis]# /etc/init.d/redis start
    bash: /etc/init.d/redis: Permission denied
    

    赋予它可执行权限:chmod +x /etc/init.d/redis

    Redis服务器设置密码后,使用/etc/init.d/redis restart会出现以下信息:

    root@localhost:/data/work# /etc/init.d/redis restart
    Stopping...
    (error) NOAUTH Authentication required.
    Waiting for Redis to shutdown...
    Waiting for Redis to shutdown...
    Waiting for Redis to shutdown...
    Waiting for Redis to shutdown...
    Waiting for Redis to shutdown...
    Waiting for Redis to shutdown...
    Waiting for Redis to shutdown...
    Waiting for Redis to shutdown...
    Waiting for Redis to shutdown...
    Waiting for Redis to shutdown...
    Waiting for Redis to shutdown...
    Waiting for Redis to shutdown...
    Waiting for Redis to shutdown...
    Waiting for Redis to shutdown...
    ...
    

    出现这样的错误信息,redis 这时是没有停止服务的。
    可以使用ps -ef | grep redis 查进程号 然后kill 掉,如果在deamon下还需要去删除pid文件,有点繁琐。
    解决办法:

    用redis-cli 密码登陆(redis-cli -a password)就OK了。
    再用ps -ef | grep redis 可以看到redis进程已经正常退出。

    修改redis服务脚本:
    vim /etc/init.d/redis
    $CLIEXEC -p $REDISPORT shutdown改成$CLIEXEC -a "password" -p $REDISPORT shutdown就可以了

    root@localhost:/data/work# /etc/init.d/redis restart
    Stopping...
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    Redis stopped
    Starting Redis server...
    6808:C 03 Jul 2020 16:41:48.479 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    6808:C 03 Jul 2020 16:41:48.479 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=6808, just started
    6808:C 03 Jul 2020 16:41:48.479 # Configuration loaded
    

    把脚本添加到系统服务列表

    [root@localhost webman]# chkconfig --add redis  // 添加
    [root@localhost webman]# chkconfig redis on    //开启
    [root@localhost webman]# chkconfig --list    //查看所有注册的脚本文件
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off
    netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
    network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
    nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off
    php-fpm         0:off   1:off   2:on    3:on    4:on    5:on    6:off
    redis           0:off   1:off   2:on    3:on    4:on    5:on    6:off
    
    

    相关文章

      网友评论

          本文标题:设置redis服务开机自启动

          本文链接:https://www.haomeiwen.com/subject/nqosqktx.html