Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

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.

share|improve this answer
    
Bingo. I moved the Wait below the Sleep, and I changed it to Sleep 5. –  Chris Sep 14 '12 at 19:59
add comment

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.

share|improve this answer
    
Nope, that's not it. –  Chris Sep 14 '12 at 19:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.