Skip to content

Instantly share code, notes, and snippets.

@fabianofa
Last active February 3, 2016 12:23
Show Gist options
  • Save fabianofa/fa29868f6e9dd846d548 to your computer and use it in GitHub Desktop.
Save fabianofa/fa29868f6e9dd846d548 to your computer and use it in GitHub Desktop.
Running multiple instances of memcached installed via Yum or RPM on CentOS 6.x
#!/bin/bash
#
# Init file for memcached
#
# Written by Dag Wieërs <[email protected]>
#
# chkconfig: - 80 12
# description: Distributed memory caching daemon
#
# processname: memcached
# config: /etc/sysconfig/memcached
# config: /etc/memcached.conf
source /etc/rc.d/init.d/functions
### Read configuration
#[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
FILES=(/etc/sysconfig/memcached_*);
# check for alternative config schema
if [ -r "${FILES[0]}" ]; then
CONFIGS=();
for FILE in "${FILES[@]}";
do
# remove prefix
#echo "${FILE} ==> file"
NAME=${FILE#/etc/sysconfig/};
# 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}]};
PIDFILE="/var/run/memcached/${NAME}.pid";
#echo "LOOP \"$CONFIG_NUM\""
#echo "config ==> ${NAME}"
#echo "$PIDFILE ==> PID"
##
RETVAL=0
#prog="memcached"
prog="${NAME}"
source "/etc/sysconfig/${NAME}"
desc="Distributed memory caching"
start() {
echo -n $"Starting $desc (${NAME}): "
daemon --pidfile $PIDFILE memcached -P $PIDFILE -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE $OPTIONS
#echo " $prog -d -p $PORT -U $PORT -u $USER -c $MAXCONN -m $CACHESIZE $OPTIONS"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Shutting down $desc ($prog): "
echo "$PIDFILE"
killproc -p $PIDFILE memcached
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $desc ($prog): "
killproc $prog -HUP
RETVAL=$?
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
reload)
reload
;;
status)
status -p $PIDFILE $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
##$RETVAL
done;
# Sample file of configuration to first instance
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1"
# Sample file of configuration to second instance
PORT="11212"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1"
# $ /etc/init.d/memcached start <configname> - Starts memcache with <configname> settings file.
$ /etc/init.d/memcached start memcached_11211
$ /etc/init.d/memcached start memcached_11212
# $ /etc/init.d/memcached start - Starts all instances of memcached possible with files prefixed with memcached_ on /etc/sysconfig/
$ /etc/init.d/memcached start
# Alternative way to start
$ service memcached start
$ service memcached start memcached_11211
$ service memcached start memcached_11212
# To stop
$ service memcached stop
$ service memcached stop memcached_11211
# To status
$ service memcached status
$ service memcached status memcached_11211
@fabianofa
Copy link
Author

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