多個 Memcached 伺服器 /etc/init.d 啟動腳本是否有效?

多個 Memcached 伺服器 /etc/init.d 啟動腳本是否有效?

我透過原始碼安裝 memcached 伺服器,並且可以為 1 個 memcached 伺服器實例安裝標準啟動腳本,但透過 google 嘗試多個腳本,無法找到一個可以管理多個 memcached 伺服器實例啟動時自動啟動的腳本。我已經嘗試過這兩個腳本,但都不起作用,service memcached start 只是返回到命令提示符,沒有啟動 memcached 伺服器實例

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

但是這個 bash 腳本可以工作,但在啟動時不會啟動 memcached 實例?

#!/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

作業系統:Centos 5.5 64位元 Memcached = v1.4.5 Memcache = v2.2.5

任何人都可以向我指出一個有效的 /etc/init.d/ 啟動腳本來管理多個 memcached 伺服器嗎?謝謝

編輯:謝謝墊子,這是最終工作的程式碼

#!/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

答案1

若要為 chkconfig 新增服務,通常需要在 shell 腳本的 shebang 下方新增一些特殊註解:

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

將這些行加入 /etc/init.d/memcached 後,您可以發出

chkconfig --add memcached

當然,進程可以啟動其他運行級別,以便檢查您是否會發出

chkconfig --list | grep "memcached"

memcached 的常見運行等級是

chkconfig --level 345 memcached on

答案2

這是我用來啟動多個 memcached 實例的腳本。關鍵點是我使用關聯數組來啟動所有或僅一個特定實例:

#! /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:我用過重新快取用於複製。

答案3

由於該腳本正在運行,我假設它沒有從 /etc/rc.d 鏈接,因此它不會在啟動時執行。假設您的啟動腳本名稱為 /etc/init.d/memcached 您應該執行

chkconfig --add memcached

將其新增至要在啟動時執行的腳本清單中。

相關內容