memcached restart shell

1.memcached-manager shell 

#!/bin/bash
###############################################################
# site: youxi.baidu.com
# author: chenqx  
# demo: ./admin_memcached.sh [start|stop|restart] [ > log.log ]
###############################################################

########################################################
#
#	CONFIGURATION
#
########################################################

# ------------------ memcached-root-path ---------------  
  ROOT=$(cd "$(dirname "$0")"; pwd) 
# ------------------ ENV-CONFIGURATION -----------------
  HOST_ADDR=$(hostname -i)  
  PORT=11211
  # max-cache-size,unit:MB
  MAX_CACHE_SIZE=4096  
  MEMCACHED_PID_FILE="$ROOT/memcached.pid"  
# ----------------- COMMAND-CONFIGURATION --------------
  MEMCACHED="$ROOT/memcached -d -m $MAX_CACHE_SIZE -p $PORT -u work -l $HOST_ADDR -P $MEMCACHED_PID_FILE"

#########################################################
# 
#      START-METHOD
#
#########################################################  

start() { 
    # get-processId by port
    existedPid=$(ps aux|grep -v grep | grep memcached | grep '11211' | awk '{printf $2}')
    # check-exist
    if [ ! -z ${existedPid} ] ; then 
	printf "memcached already running,exit 1\n"
        exit 1 ;		
    else
	printf "starting memcached\n"
	$MEMCACHED
	printf "memcached-started,sleep 2 seconds\n"  
	sleep 2  
 
	printf "checking memcached-avariable\n"
	${ROOT}/check_memcached.sh ${HOST_ADDR} ${PORT}
	checkCode=$?
	if [ $checkCode -ne 0 ];then
	    printf "check-memcached failed,retcode is $checkCode\n" 
	    exit -1 
	else
	    printf "check-memcached ok\n"
	    PID=$(cat $MEMCACHED_PID_FILE)  
	    printf "memcached is started PID:$PID\n"
	fi  
	
    fi
}

#########################################################
#
#      START-METHOD(ProcessId By PID-FILE)
#
#########################################################

start_PidFile() {
    # start memcached
    if [ -f "$MEMCACHED_PID_FILE" ] && [ -s "$MEMCACHED_PID_FILE" ]; then
        printf "memcached already running\n"
        exit 1 ;
    else
        printf "starting memcached\n"
        $MEMCACHED
 
        sleep 2

        printf "checking memcached-avariable \n"
        ${ROOT}/check_memcached.sh ${HOST_ADDR} ${PORT}
        checkCode=$?
        if [ $checkCode -ne 0 ];then
            printf "check-memcached failed,retcode is $checkCode\n"
            exit -1
        else
            printf "check-memcached ok\n"
            PID=$(cat $MEMCACHED_PID_FILE)
            printf "memcached is started PID:$PID\n"
        fi

    fi
}  

#########################################################
# 
#      STOP-METHOD
#
#########################################################   

stop() {  
	printf "stoping memcached\n"  
	# get processId by ps-port
	existedPid=$(ps aux|grep -v grep | grep memcached | grep '11211' | awk '{printf $2}')
	# check-processId
	if [ -z ${existedPid}  ] ; then 
		printf "no memcached running,exit 1\n" 
		exit 1 
	else
		kill -9 ${existedPid}
		# rm pid-file
		if [ -f "$MEMCACHED_PID_FILE" ] && [ -s "$MEMCACHED_PID_FILE" ]; then  
	            rm -f ${MEMCACHED_PID_FILE}  
        	fi
		
        	printf "memcached is stopped\n"  
    	fi
}

#########################################################
#
#      STOP-METHOD(processId By Pid-File)
#
#########################################################

stop_PidFile() {
    printf "stoping memcached\n"
    if [ -f "$MEMCACHED_PID_FILE" ] && [ -s "$MEMCACHED_PID_FILE" ]; then
        PID=$(cat $MEMCACHED_PID_FILE)
        rm -f ${MEMCACHED_PID_FILE}
        if [ ! -z ${PID} ]; then
            kill -9 ${PID}
        fi
        printf "memcached is stopped\n"
    else
        printf "no memcached running\n"
        exit 1
    fi
}

#########################################################
#
#      CALL-METHOD
#
######################################################### 
case "$1" in  
    start)  
        start  
        ;;  
    stop)  
        stop  
        ;;  
    restart)  
        stop  
        sleep 3  
        start  
        ;;  
    *)  
        printf "Usage:$0 {start|stop|restart}\n"  
        exit 2
esac
exit 0 


 2.check-memcached with telnet

#! /usr/bin/expect
################################################
# group: youxi.baidu.com
# author: chenqx
# demo: ./check_memcached.sh 127.0.0.1 11211
# args-within-header: -d debug
##################################################
# ----  log   ----
send_user "telnet [lindex $argv 0] [lindex $argv 1] \n"
# ----command ----
spawn telnet [lindex $argv 0] [lindex $argv 1]
set timeout 10
# ---- telnet ----
expect {
 eof {exit 4}
 timeout {exit 5}
 "*Connected*" {send "version\r"}
 "*refused"  {send_user "telnet-refused\n";exit 1}
}
# ---- version ----
expect {
  eof {exit 4}
  timeout {exit 5}
  "*VERSION*" {send_user "service ok\n";exit 0}
  "*error*"  {send_user "cmd-error\n";exit 2}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值