Skip navigation

While re-factoring a project for performance and improved consistency, it became necessary to store objects in memory for later use to avoid serialization and anything to do with the session. Memcached to the resue? Somewhat, had to do this one by hand as the environment this project is to be deployed within has a number of custom attributes including custom compiled binaries for PHP, libmemcached, imagick, XCache, gearman, etc. Configuring and compiling memcached couldn’t have been easier:

./configure && make && sudo make install && sudo adduser memcached

(Remember to add the new user memcached will run under, using ‘nobody’ is considered to be unsafe.) Now I needed to make sure the Memcache server would start when runlevel=3, after some experimentation this is the bash script that resulted:

#! /bin/sh
#
# chkconfig: - 55 45
# description: The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached
# pidfile: /var/run/memcached/memcached.pid
# author: Jack Brain
# Standard LSB functions
#. /lib/lsb/init-functions
# Source function library.
. /etc/init.d/functions
PORT=11211
USER=memcached
MAXCONN=2048
CACHESIZE=128
OPTIONS=''
if [ -f /etc/sysconfig/memcached ];then
. /etc/sysconfig/memcached
fi
# Check that networking is up.
. /etc/sysconfig/network
if [ "$NETWORKING" = "no" ]
then
exit 0
fi
RETVAL=0
prog=”memcached”
pidfile=${PIDFILE-/var/run/memcached/memcached.pid}
lockfile=${LOCKFILE-/var/lock/subsys/memcached}
start () {
echo -n $”Starting $prog: “
# Ensure that /var/run/memcached has proper permissions
if [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then
chown $USER /var/run/memcached
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: “
killproc -p ${pidfile} 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
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
exit $RETVAL

Leave a Reply