I just need Memcached to start automatically when reboot so that I don't have to wait to log into the server to do that every time I restart my server. The problem here is that I didn't install Memcached using YUM. I compiled Memcached it myself. So, there is no startup script for me to do it. Also, from what I have read, init.d startup script comes with YUM only support one instance of Memcached. In my case, I need to run multiple instances of Memcached. So, I searched for the script and below script is work for me
1. Create Script in /etc/init.d/memcached
#!/bin/bash # # Init file for memcached # # Written by Dag Wieërs # # 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 /usr/local/bin/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;
Note that when install Memcached using YUM, the path of Memcached is /usr/bin/memcached but if you compiled it by yourself, the path will be /usr/local/bin/memcached. The latter is what I put in the script.
2. CHMOD memcached startup script to 755
chmod 755 /etc/init.d/memcached
3. Create Memcached configuration files. You can created as many as you want in /etc/sysconfig/. The format of the file has to be memcached_PORT like memcached_11211, memcache_11212 .... Each file will contain configuration below :
memcached_11211
PORT="11211" USER="nobody" MAXCONN="2048" CACHESIZE="64" OPTIONS=""
memcached_11212
PORT="11212" USER="nobody" MAXCONN="2048" CACHESIZE="256" OPTIONS="-L -l 127.0.0.1"
You can add addition option in OPTIONS. Something like -L
4. Create a folder to keep all Memcached PID files :
mkdir /var/run/memcached chmod 755 /var/run/memcached chown nobody:nobody /var/run/memcached
5. Usage :
Startup command
/etc/init.d/memcached start /etc/init.d/memcached start memcached_11211 service memcached start service memcached start memcached_11211
Status command
/etc/init.d/memcached status /etc/init.d/memcached status memcached_11211 service memcached status service memcached status memcached_11211
Stop command
/etc/init.d/memcached stop /etc/init.d/memcached stop memcached_11211 service memcached stop service memcached stop memcached_11211
6. Now you may need Memcached to startup when your computer reboot. Here is what I did :
chkconfig --level 345 memcached on
Then, check if memcached is already added to chkconfig
chkconfig --list | grep "memcached"
Below is what I got :
# chkconfig --list | grep "memcached" memcached 0:off 1:off 2:off 3:on 4:on 5:on 6:off
Server Information - Software
- CentOS 5.6 - 64 bits
- Memcached 1.4.6
Source :
- http://addmoremem.blogspot.com/2010/09/running-multiple-instances-of-memcached.html