3

I can't shut down my redis server via the init script. This has the huge side effect of hanging my machine when I do sudo reboot.

I freshly installed redis using the canonical guide, configured it to accept connections on a unix socket, and am now attempting to stop the server so that I can restore a previously saved .rdb dump.

But I keep getting the following:

Could not connect to Redis at 127.0.0.1:0: Connection refused
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 ...

The command I'm using is sudo /etc/init.d/redis-server stop. Note that I can still issue shutdown after connecting to redis-cli -s /var/run/redis.sock, but I need the init script to work.

Why won't redis shut down, and how can I fix this situation? The version is Redis server v=4.0.2 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=8ddba2e6a24ce078


I've naturally set port 0 in the config file, alongwith unixsocket /var/run/redis.sock and unixsocketperm 755.

Moreover, the contents of the script file are:

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

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

PIDFILE=/var/run/redis.pid
CONF="/etc/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
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

Note REDISPORT=0, as it should be. Currently, this doesn't really support stopping and starting the redis server.

Hassan Baig
  • 15,055
  • 27
  • 102
  • 205

1 Answers1

0

I made customizations to the init script posted in the question. I had to make redis' init script support unix socket based shut down. It doesn't support that OOTB.

Here's how my script ended up looking:

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

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

REDISSOCKET="/var/run/redis.sock"

PIDFILE=/var/run/redis.pid
CONF="/etc/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
                echo "Stopping ..."
                PID=$(cat $PIDFILE)
                if [ $REDISPORT -eq 0 ] && [ -n "$REDISSOCKET" ]
                then
                        $CLIEXEC -s $REDISSOCKET shutdown
                else
                        $CLIEXEC -p $REDISPORT shutdown
                fi
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

Once you've changed the script, trying issuing sudo service redis-server stop. If the redis-server was added to the necessary run levels, it should work.

Otherwise just try sudo /etc/init.d/redis-server stop.


Note: a similar problem was solved here: https://groups.google.com/forum/#!topic/redis-db/o-fC_B-fjPs

Hassan Baig
  • 15,055
  • 27
  • 102
  • 205