Skip to content

Instantly share code, notes, and snippets.

@tegansnyder
Created June 10, 2014 17:57
Show Gist options
  • Save tegansnyder/59fd3fc44d1a8448945d to your computer and use it in GitHub Desktop.
Save tegansnyder/59fd3fc44d1a8448945d to your computer and use it in GitHub Desktop.
Redis setup script for multiple instances on different ports
#!/bin/bash
# make sure this script is executable
# place in /var/redis/
# run by issuing: ./redis-setup.sh PORT_NUMBER
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
if [ $# -gt 0 ]; then
REDISPORT=$1
cat redis.init.conf | sed -e "s/\${REDISPORT}/$REDISPORT/" > /etc/init.d/redis_$REDISPORT
chmod 755 /etc/init.d/redis_$REDISPORT
echo "Redis startup script created at: /etc/init.d/redis_$REDISPORT"
cat redis.setup.conf | sed -e "s/\${REDISPORT}/$REDISPORT/" > /etc/redis/$REDISPORT.conf
echo "Redis config created at: /etc/redis/$REDISPORT.conf"
mkdir /var/redis/$REDISPORT
chmod 775 /var/redis/$REDISPORT
echo "Redis data at: /var/redis/$REDISPORT"
/etc/init.d/redis_$REDISPORT start
echo
echo "Ping your host to make sure it works by issuing:"
echo "redis-cli -p $REDISPORT"
else
echo 'No port number given'
fi
@tegansnyder
Copy link
Author

redis.init.conf

#!/bin/sh
#
# redis        Startup script for Redis Server
#
# chkconfig: - 2345 95 20
# description: Redis is an open source, advanced key-value store. 
#
# processname: redis-server

REDISPORT=${REDISPORT}
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.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

exit 0

@tegansnyder
Copy link
Author

redis.setup.conf

daemonize yes
pidfile /var/run/redis_${REDISPORT}.pid
port ${REDISPORT}
timeout 0
tcp-keepalive 0
loglevel notice
logfile /var/log/redis_${REDISPORT}.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/redis/${REDISPORT}
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
maxmemory 3gb
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

@tegansnyder
Copy link
Author

Instructions to install Redis:

wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
make install

Then run the above setup script to create multiple instances of Redis running on separate ports.

sudo sh setup.sh PORT_NUMBER

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment