我通过源代码安装了 memcached 服务器,并可以为 1 个 memcached 服务器实例安装标准启动脚本,但通过谷歌尝试了几个脚本,找不到一个可以管理多个 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 服务器吗?谢谢
编辑:谢谢 mat,这是最终有效的代码
#!/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
附言:我用过复制缓存进行复制。
答案3
由于脚本正在运行,我假设它没有从 /etc/rc.d 链接,因此在启动时不会执行。假设您的启动脚本名为 /etc/init.d/memcached,您应该执行
chkconfig --add memcached
将其添加到启动时运行的脚本列表中。