Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

#Check to see if Redis is running
killall -0 redis-server
if [ $? -eq 1 ]
                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 ]
                                echo redis master not running. Doing nothing. >> /var/lib/redis/log.txt
                                echo redis master is running. I must be the slave. Restarting keepalived >> /var/lib/redis/log.txt
                                redis-server ${REDIS_CONF}
                                sleep 1
                                redis-cli SLAVEOF ${REDIS_IP} 6379
                echo redis-server running. >> /var/lib/redis/log.txt

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

redis-cli SLAVEOF 6379

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

share|improve this question
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

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

Your Answer


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.