6

I install memcached server via source and can get standard start up script installed for 1 memcached server instance, but trying several scripts via google, can't find one that works to manager auto start up on boot for multiple memcached server instances. I've tried both these scripts and both don't work, service memcached start just returns to command prompt with no memcached server instances started

  • lullabot.com/articles/installing-memcached-redhat-or-centos
  • addmoremem.blogspot.com/2010/09/running-multiple-instances-of-memcached.html

However this bash script works but doesn't start up memcached instances at start up though ?

#!/bin/sh case "$1" in start) /usr/local/bin/memcached -d -m 16 -p 11211 -u nobody /usr/local/bin/memcached -d -m 16 -p 11212 -u nobody ;; stop) killall memcached ;; esac 

OS: Centos 5.5 64bit Memcached = v1.4.5 Memcache = v2.2.5

Anyone can point me to a working /etc/init.d/ startup script to manage multiple memcached servers ? Thanks

edit: Thanks mat, this is code that ended up working

#!/bin/sh # chkconfig: - 80 12 # description: The memcached daemon is a network memory cache service. # processname: memcached BIN=/usr/local/bin/memcached USER=nobody CON=2048 THREADS=4 $BIN -d -m 16 -p 11211 -c $CON -t $THREADS -u $USER $BIN -d -m 16 -p 11212 -c $CON -t $THREADS -u $USER case "$1" in start) $BIN -d -m 16 -p 11211 -c $CON -t $THREADS -u $USER $BIN -d -m 16 -p 11212 -c $CON -t $THREADS -u $USER ;; stop) killall $BIN ;; esac 

3 Answers 3

5

To add a service to chkconfig you will normally need a couple of special comments below the shebang of a shell script:

#!/bin/sh # chkconfig: - 55 45 # description: The memcached daemon is a network memory cache service. # processname: memcached 

After adding the lines to /etc/init.d/memcached you can then issue

chkconfig --add memcached 

There are of course additional run levels a process can start at so to check that you would issue

chkconfig --list | grep "memcached" 

A common run level for memcached would be

chkconfig --level 345 memcached on 
1
  • thanks mat i did try copying and pasting someone elses comments below the shebang of the script, but that didn't work. But copying and pasting yours did work! Code i ended up using for /etc/init.d/memcached which works on reboot/startup is edited above in original post. Not sure if syntax is correct but it works :) Commented Dec 24, 2010 at 3:10
1

Here's the script that I'm using to start multiple memcached instances. The key point is I use associative array to start all or just one specific instance:

#! /usr/local/bash4/bin/bash # # chkconfig: - 55 45 # description: The memcached daemon is a network memory cache service. # processname: memcached # config: /etc/sysconfig/memcached # Standard LSB functions #. /lib/lsb/init-functions # Source function library. . /etc/init.d/functions 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 typeset -A PIDS typeset -A MEMORYS typeset -A FACTORS typeset -A INSTANCES PORTS="11216 11217" USER=memcached basedir="/usr/local/memcached" MAXCONN=1024 OPTIONS="" INSTANCES=([11216]="session" [11217]="userdata") PIDS=([11216]="$basedir/var/run/sohaphimdetails.pid" [11217]="$basedir/var/run/sohamuzik-top.pid") MEMORYS=([11216]="1024" [11217]="2048") FACTORS=([11216]="1.25" [11217]="1.125") RETVAL=0 prog="memcached" cmd="/usr/local/memcached/bin/memcached" user="memcached" ip="192.168.6.66" peer="192.168.6.28" max_conn=2048 max_memory=512 threads=8 function start() { port="$1" if [ `ps -ef | grep "$cmd" | grep -c $port` -ge 1 ]; then action $"Starting the memcached server on port '$port'... " /bin/false else if [ ! -f $basedir/var/log/${INSTANCES[$port]}.log ]; then touch $basedir/var/log/${INSTANCES[$port]}.log /bin/chown memcached:memcached $basedir/var/log/${INSTANCES[$port]}.log fi $cmd -d -u $user -l $ip -c $max_conn -t $threads -m ${MEMORYS[$port]} -p $port -P $basedir/var/run/${INSTANCES[$port]}.pid -f ${FACTORS[$port]} -x $peer -X `expr $port '*' 10` -vv > $basedir/var/log/${INSTANCES[$port]}.log 2>&1 action $"Starting the memcached server on port '$port'... " /bin/true fi } function stop() { port="$1" if [ `ps -ef | grep "$cmd" | grep -c $port` -eq 0 ]; then action $"Stopping the memcached server on port '$port'... " /bin/false else kill -TERM `ps -ef | grep "$cmd" | grep $port | grep -v grep | awk '{ print $2 }'` action $"Stopping the memcached server on port '$port'... " /bin/true fi } case "$1" in start) if [ -n "$2" ]; then start $2 else for port in $PORTS; do start $port done fi ;; stop) if [ -n "$2" ]; then port="$2" stop $port else killproc $prog fi ;; restart) if [ -n "$2" ]; then stop $2 start $2 else for port in $PORTS; do stop $port start $port done fi ;; *) printf 'Usage: %s {start|stop|restart} <port>\n' "$prog" exit 1 ;; esac 

PS: I used repcached for replication.

0

Since the script is working, I assume that it is not linked from /etc/rc.d, so it is not executed at startup. Assuming that your startup script is called /etc/init.d/memcached you should execute

chkconfig --add memcached 

to add it to the list of scripts to be run at startup.

2
  • thanks i have already ran chkconfig --add memcached but still doing service memcached start results in no memcached servers started at all ? Commented Dec 23, 2010 at 15:02
  • Mat was more precise than me. Check runlevels with chkconfig and make sure that the script is executed in the right runlevels. If it is, check logs for error messages. Commented Dec 23, 2010 at 15:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.