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}
}