Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Init-script for optionally starting multiple redis instances on the same host running Ubunut/Debian. Highly inspired by the memcached init script that comes with the Ubuntu package. This is useful since redis is single-threaded.
#! /bin/bash
### BEGIN INIT INFO
# Provides: redis-server
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis-server - Persistent key-value db
# Description: redis-server - Persistent key-value db
### END INIT INFO
# Usage:
# cp /etc/redis/redis.conf /etc/redis/redis_server1.conf
# cp /etc/redis/redis.conf /etc/redis/redis_server2.conf
# start all instances:
# /etc/init.d/redis-server start
# start one instance:
# /etc/init.d/redis-server start server1
# stop all instances:
# /etc/init.d/redis-server stop
# stop one instance:
# /etc/init.d/redis-server stop server1
#
# Make sure to set the pidfile option correctly in the configs, e.g. for the
# example above in /etc/redis/redis_server1.conf set:
# pidfile /var/run/redis/redis_server1.pid
# and in /etc/redis/redis_server2.conf set:
# pidfile /var/run/redis/redis_server2.pid
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/redis-server
NAME=redis-server
DESC=redis-server
RUNDIR=/var/run/redis
test -x $DAEMON || exit 0
set -e
test -r /etc/default/$NAME && . /etc/default/$NAME
FILES=(/etc/redis/redis_*.conf)
# check for alternative config schema
if [ -r "${FILES[0]}" ]
then
CONFIGS=()
for FILE in "${FILES[@]}"
do
# remove prefix
NAME=${FILE#/etc/redis/}
# remove suffix
NAME=${NAME%.conf}
# check optional second param
if [ $# -ne 2 ]
then
# add to config array
CONFIGS+=($NAME)
elif [ "redis_$2" == "$NAME" ]
then
# use only one redis
CONFIGS=($NAME)
break
fi
done
if [ ${#CONFIGS[@]} == 0 ];
then
echo "Config not exist for: $2" >&2
exit 1
fi
else
CONFIGS=(redis)
fi
CONFIG_NUM=${#CONFIGS[@]}
for ((i=0; i < $CONFIG_NUM; i++))
do
NAME=${CONFIGS[${i}]}
PIDFILE="/var/run/redis/${NAME}.pid"
case "$1" in
start)
echo -n "Starting $DESC: "
mkdir -p $RUNDIR
touch $PIDFILE
chown redis:redis $RUNDIR $PIDFILE
chmod 755 $RUNDIR
if [ -n "$ULIMIT" ]
then
ulimit -n $ULIMIT
fi
if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid redis:redis --exec $DAEMON -- /etc/redis/${NAME}.conf
then
echo "$NAME."
else
echo "failed"
fi
;;
stop)
echo -n "Stopping $DESC: "
if start-stop-daemon --stop --retry forever/TERM/1 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
then
echo "$NAME."
else
echo "failed"
fi
rm -f $PIDFILE
sleep 1
;;
restart|force-reload)
ARGS=($@)
CONFIG=${ARGS[@]:1}
${0} stop $CONFIG
${0} start $CONFIG
exit 0
;;
status)
echo -n "$DESC ($NAME) is "
if start-stop-daemon --stop --quiet --signal 0 --pidfile $PIDFILE --exec $DAEMON
then
echo "running"
else
echo "not running"
fi
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac
done
exit 0
@paramadeep
Copy link

paramadeep commented Apr 15, 2015

the post was helpful

@Lewiscowles1986
Copy link

Lewiscowles1986 commented Aug 26, 2015

This is pretty awesome 👍

I wonder if a utility for configuring redis server configs interactively and from CLI would be needed to get this to upstream, or would add usability to multi-redis setup, like the a2enmod and a2ensite apache can enjoy.

@abrugh
Copy link

abrugh commented Nov 24, 2015

doesn't this only work for a single instance as exec is basically switching the process from this script to whatever's being exec-ed?

@sethuper
Copy link

sethuper commented Dec 12, 2015

Script was helpful, base on it I have made new script for 8 instances for privoxy server.. TY.

http://terminal28.com/anonymity-online-how-to-install-and-configure-squid3-tor-privoxy-debian-ubuntu-linux/

@pranaysonisoft
Copy link

pranaysonisoft commented Feb 29, 2016

How to Use this Script. i am new in Ubuntu.

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