Multiple Memcached server /etc/init.d startup script that works?

Solution 1:

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

Solution 2:

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.