6bfd36
#!/bin/bash
6bfd36
#
6bfd36
# Wrapper to close properly redis and sentinel
6bfd36
test x"$REDIS_DEBUG" != x && set -x
6bfd36
6bfd36
REDIS_CLI=/usr/bin/redis-cli
6bfd36
6bfd36
# Retrieve service name
6bfd36
SERVICE_NAME="$1"
6bfd36
if [ -z "$SERVICE_NAME" ]; then
6bfd36
   SERVICE_NAME=redis
6bfd36
fi
6bfd36
6bfd36
# Get the proper config file based on service name
6bfd36
CONFIG_FILE="/etc/$SERVICE_NAME.conf"
6bfd36
6bfd36
# Use awk to retrieve host, port from config file
6bfd36
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
6bfd36
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
6bfd36
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
6bfd36
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`
6bfd36
6bfd36
# Just in case, use default host, port
6bfd36
HOST=${HOST:-127.0.0.1}
6bfd36
if [ "$SERVICE_NAME" = redis ]; then
6bfd36
    PORT=${PORT:-6379}
6bfd36
else
6bfd36
    PORT=${PORT:-26739}
6bfd36
fi
6bfd36
6bfd36
# Setup additional parameters
6bfd36
# e.g password-protected redis instances
6bfd36
[ -z "$PASS"  ] || ADDITIONAL_PARAMS="-a $PASS"
6bfd36
6bfd36
# shutdown the service properly
6bfd36
if [ -e "$SOCK" ] ; then
6bfd36
	$REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
6bfd36
else
6bfd36
	$REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
6bfd36
fi