2

My organization has Redis running in a Master/Slave configuration, using Keepalived to switch between the two. It was discovered that when the Slave instance dies for some reason, Keepalived will not restart it. I started writing a cron job to check and see if redis is running, and if the slave dies, to restart it.

#!/bin/bash
#redis_cron_restart.sh
######variables########
REDIS_IP="10.19.105.229"
REDIS_CONF="/var/lib/redis/redis.conf"
#Check to see if Redis is running
killall -0 redis-server
if [ $? -eq 1 ]
        then
                date >> /var/lib/redis/log.txt
                echo redis-server not running. Checking redis master >> /var/lib/redis/log.txt
                redis-cli -h ${REDIS_IP} PING
                if [ $? -eq 1 ]
                        then
                                echo redis master not running. Doing nothing. >> /var/lib/redis/log.txt
                        else
                                echo redis master is running. I must be the slave. Restarting keepalived >> /var/lib/redis/log.txt
                                redis-server ${REDIS_CONF}
                                sleep 1
                                wait
                                redis-cli SLAVEOF ${REDIS_IP} 6379
                fi
        else
                echo redis-server running. >> /var/lib/redis/log.txt
fi

It runs, and it restarts Redis. It does not, however, put Redis into slave mode. If I type

redis-cli SLAVEOF 10.19.105.229 6379

at my terminal, however, it goes into slave mode. Any ideas?

Chris
  • 347
  • 3
  • 6
  • 13

2 Answers2

6

Is it possible that, in your innermost else block, the call to redis-server is returning immediately, but the server itself is taking more than one second to start up? In that case, wait won't wait long enough.

2

Have you tried writing the complete path to redis-cli and redis-server in the script? Or simply set the PATH variable at the beginning of the script and make it the same that you see when you type echo $PATH in your terminal.

r.t.
  • 161
  • 3