Redis start/stop init.d script for Linux

The init script that came with Redis (open source, advanced key-value store server) didn’t work and I couldn’t find one for RedHat/CentOS with a quick search, so I combined the parts that did and came up with one that seems to work both for start/stop via /sbin/service as well as /sbin/chkconfig. Note: at a minimum, you must set the “daemonize” value in the redis.conf file to “yes”.

Use the redis.conf file comes with the distro — edit as needed and copy it to: /etc/redis.conf.
You can then use the script below and copy that to: /etc/init.d/redis (change the port and any other attributes as needed). (To run multiple servers, change the port (and pid) values as appropriate.)

Click here to view init.d script for Redis

#!/bin/sh
#
# redis        Startup script for Redis Server
#
# chkconfig: - 90 10
# description: Redis is an open source, advanced key-value store. 
#
# processname: redis-server
# config: /etc/redis.conf
# pidfile: /var/run/redis_6379.pid
 
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
 
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=`which redis-cli`
 
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis.conf"
 
case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo -n "$PIDFILE exists, process is already running or crashed\n"
        else
                echo -n "Starting Redis server...\n"
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo -n "$PIDFILE does not exist, process is not running\n"
        else
		PID=$(cat $PIDFILE)
                echo -n "Stopping ...\n"
				$REDIS_CLI -p $REDISPORT SHUTDOWN
                while [ -x ${PIDFILE} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
esac

Leave a Reply