首页 > Redis使能密码后Ubuntu虚拟机无法正常关机

Redis使能密码后Ubuntu虚拟机无法正常关机

我一直在虚拟机中开发自己的服务器程序。

Redis缺省是没有密码的,我使能密码后,发现虚拟机无法正常关机。关机时Redis报告脚本密码错误,并一直死循环。

具体看截图。有:(error) NOAUTH Authentication required.

但是我不知道redis停止脚本在哪里。求帮助。


Redis可以通过客户端执行SHUTDOWN命令进行关闭。


我也遇到过一样的问题。
我当时的情况是:使用puppet做redis的自动下发和部署,在为redis启用认证密码后,在puppet中关闭服务,puppet会卡死无法继续。后来手动定位问题也发现了同样的事情。
原因其实是redis作者在源码包里提供的服务控制脚本没有考虑到加认证密码后如何关闭服务。
带密码的关闭方式是:redis-cli -p 端口 -a 密码 shutdown
因为我是用puppet做redis的部署,所以把这个脚本做成了puppet文件模板,如果加了密码就会自动修改服务控制脚本。我把模板贴上来,希望有所帮助。

#!/bin/sh
#Configurations injected by install_server below....

EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_<%= name %>.pid
CONF="/etc/redis/<%= name %>.conf"
REDISPORT="<%= name %>"
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_<%= name %> is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_<%= name %>
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop redis_<%= name %>
# Description: Redis daemon
### END INIT INFO


case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
            echo "$PIDFILE exists, process is already running or crashed"
        else
            echo "Starting Redis_$REDISPORT server..."
            $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
            echo "$PIDFILE does not exist, process is not running"
        else
            PID=$(cat $PIDFILE)
            echo "Stopping ..."
            <% if @password == 'nopass' -%>
            $CLIEXEC -p $REDISPORT shutdown
            <% else -%>
            $CLIEXEC -p $REDISPORT -a <%= @password %> shutdown
            <% end -%>
            while [ -x /proc/${PID} ]
            do
                echo "Waiting for Redis_$REDISPORT to shutdown ..."
                sleep 2
            done
            echo "Redis_$REDISPORT stopped"
        fi
        ;;
    status)
        PID=$(cat $PIDFILE)
        if [ -f $PIDFILE ]
        then
          if [ ! -x /proc/${PID} ]
          then
            echo 'Redis_$REDISPORT is not running'
            rm -rf $PIDFILE
            exit 1
          else
            echo "Redis_$REDISPORT is running"
          fi
        else
          echo 'No PID File,Redis_$REDISPORT is not running'
          exit 1
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Please use start, stop, restart or status as first argument"
        ;;
esac

目前我暂时做了一个关闭Redis的脚本,关闭虚拟机之前手动运行一次。有点儿搓。

因为Shell编程不算熟悉,所以还需要学习一下您的脚本。不过redis-cli如果没有密钥应该会返回NOAUTH错误,需要捕获后做判断重新使用redis-cli+密钥来关闭Redis。

最后在/etc/init目录中去寻找对应shutdown脚本看看如何整合。

【热门文章】
【热门文章】