Skip to content

Instantly share code, notes, and snippets.

@Rydgel
Forked from ctavan/gist:4482825
Last active August 29, 2015 14:21
Show Gist options
  • Save Rydgel/c334ae978397551809e0 to your computer and use it in GitHub Desktop.
Save Rydgel/c334ae978397551809e0 to your computer and use it in GitHub Desktop.
#! /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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment