Created
April 9, 2017 07:07
-
-
Save stewartadam/ce5cacd771e62002088a2d851fde6853 to your computer and use it in GitHub Desktop.
memcached-multi, a RHEL6 initscript with support for easily managing multiple memcached services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# | |
# chkconfig: - 55 45 | |
# description: The memcached daemon is a network memory cache service. | |
# processname: memcached | |
### END INIT INFO | |
# Based on https://gist.github.com/lboynton/3775818 | |
# Usage: | |
# cp /etc/sysconfig/memcached /etc/sysconfig/memcached-server1 | |
# cp /etc/sysconfig/memcached.conf /etc/sysconfig/memcached-server2 | |
# start all instances: | |
# /etc/init.d/memcached-multi start | |
# start one instance: | |
# /etc/init.d/memcached-multi start server1 | |
# stop all instances: | |
# /etc/init.d/memcached-multi stop | |
# stop one instance: | |
# /etc/init.d/memcached-multi stop server1 | |
# Source function library. | |
. /etc/rc.d/init.d/functions | |
# Check that networking is up. | |
. /etc/sysconfig/network | |
# Check that networking is up. | |
if [ "$NETWORKING" = "no" ] | |
then | |
exit 0 | |
fi | |
prog="memcached" | |
FILES=(/etc/sysconfig/$prog-*) | |
if [ -r "${FILES[0]}" ]; then | |
CONFIGS=() | |
for FILE in "${FILES[@]}"; do | |
# remove prefix | |
NAME=${FILE#/etc/sysconfig/} | |
# remove suffix | |
NAME=${NAME%.conf} | |
NAME=${NAME#$prog-} | |
# check optional second param | |
if [ $# -ne 2 ]; then | |
# add to config array | |
CONFIGS+=($NAME) | |
elif [ "$2" == "$NAME" ]; then | |
# use only one memcached | |
CONFIGS=($NAME) | |
break; | |
fi | |
done | |
if [ ${#CONFIGS[@]} == 0 ]; then | |
echo "Config not exist for: $2" >&2 | |
exit 1 | |
fi | |
else | |
CONFIGS=(memcached) | |
fi | |
CONFIG_NUM=${#CONFIGS[@]} | |
for ((i=0; i < $CONFIG_NUM; i++)); do | |
NAME=${CONFIGS[${i}]} | |
# start loop | |
if [ -f "/etc/sysconfig/memcached-$NAME.conf" ];then | |
. "/etc/sysconfig/memcached-$NAME.conf" | |
fi | |
pidfile=${PIDFILE-/var/run/memcached/memcached-$NAME.pid} | |
lockfile=${LOCKFILE-/var/lock/subsys/memcached-$NAME} | |
start () { | |
echo -n $"Starting $prog ($NAME): " | |
# Ensure that $pidfile directory has proper permissions and exists | |
piddir=`dirname $pidfile` | |
if [ ! -d $piddir ]; then | |
mkdir $piddir | |
fi | |
if [ "`stat -c %U $piddir`" != "$USER" ]; then | |
chown $USER $piddir | |
fi | |
daemon --pidfile ${pidfile} memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P ${pidfile} $OPTIONS | |
RETVAL=$? | |
echo | |
[ $RETVAL -eq 0 ] && touch ${lockfile} | |
} | |
stop () { | |
echo -n $"Stopping $prog ($NAME): " | |
killproc -p ${pidfile} /usr/bin/memcached | |
RETVAL=$? | |
echo | |
if [ $RETVAL -eq 0 ] ; then | |
rm -f ${lockfile} ${pidfile} | |
fi | |
} | |
restart () { | |
stop | |
start | |
} | |
# See how we were called. | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
status) | |
status -p ${pidfile} memcached-$NAME | |
RETVAL=$? | |
;; | |
restart|reload|force-reload) | |
restart | |
;; | |
condrestart|try-restart) | |
[ -f ${lockfile} ] && restart || : | |
;; | |
*) | |
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart}" | |
RETVAL=2 | |
;; | |
esac | |
# end loop | |
done; | |
exit $? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment