sh memcached 进程启动及监控

memcached 进程启动及监控

1.memcached_inc.sh
设置路径,端口等讯息。

#!/bin/sh

#config include

HOST=$(hostname)
SITE="mysite"
PORT=11211

MEMCACHED_PID_FILE="/tmp/memcached.pid"
MEMCACHED_DAEMON_PID_FILE="/tmp/memcached_daemon.pid"

MEMCACHED="memcached -d -m 64 -p $PORT -u memcache -l 127.0.0.1 -P $MEMCACHED_PID_FILE"
MEMCACHED_DAEMON_FILE="memcached_daemon.sh"

ERROR_LOG_FILE="${ROOT}/memcached_${SITE}_${HOST}_${PORT}.log"

2.gm_memcached.sh
控制memcached 启动,停止,重启。

#!/bin/sh

#memcached start and stop
#$1 action

ROOT=$(cd "$(dirname "$0")"; pwd)

. ${ROOT}/memcached_inc.sh


start() {

    if [ -f "$MEMCACHED_PID_FILE" ] && [ -s "$MEMCACHED_PID_FILE" ]; then
        printf "memcached already running\n"
    else
        printf "starting memcached\n"
        $MEMCACHED

        sleep 2

        PID=$(cat $MEMCACHED_PID_FILE)
        printf "memcached is started PID:$PID\n"

        printf "starting memcached daemon\n"
        ${ROOT}/${MEMCACHED_DAEMON_FILE} &
        DAEMON_PID=$!
        echo ${DAEMON_PID} > ${MEMCACHED_DAEMON_PID_FILE}
        printf "memcached daemon is started PID:${DAEMON_PID}\n"
    fi

}


stop() {

    if [ -f "$MEMCACHED_DAEMON_PID_FILE" ] && [ -s "$MEMCACHED_DAEMON_PID_FILE" ]; then
        DAEMON_PID=$(cat $MEMCACHED_DAEMON_PID_FILE)
        rm -f ${MEMCACHED_DAEMON_PID_FILE}
        if [ ! -z ${DAEMON_PID} ]; then
            kill -9 ${DAEMON_PID}
        fi
        printf "memcached daemon is stopped\n"
    else
        printf "no memcached daemon running\n"
    fi

    sleep 1

    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"
    fi

}


case "$1" in

    start)
        start
        ;;

    stop)
        stop
        ;;

    restart)
        stop
        sleep 3
        start
        ;;

    *)
        printf "Usage:$0 {start|stop|restart}\n"
        exit 1  

esac

exit 0

3.memcached_daemon.sh
监控memcached 进程,如进程失效则自动启动。

#!/bin/sh

#memcached daemon

ROOT=$(cd "$(dirname "$0")"; pwd)

. ${ROOT}/memcached_inc.sh


while :
do
    if [ -f "$MEMCACHED_PID_FILE" ] && [ -s "$MEMCACHED_PID_FILE" ]; then
        PID=$(cat $MEMCACHED_PID_FILE)
    else
        PID=""
    fi
   
    if [ -z "$PID" ] || [ -z $(ps aux|awk '{print $2}' | grep "^$PID$") ]; then
        $MEMCACHED
        sleep 1
        printf "[$(date +%Y-%m-%d' '%H:%M:%S)] ${SITE} ${HOST} memcached ${PORT} is restarted\n" >> $ERROR_LOG_FILE
        echo "Subject: ${SITE} ${HOST} memcached ${PORT} is restarted $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail me@gmail.com
    fi

    sleep 5

done

exit 0

使用方法:
./gm_memcached.sh start   #启动memcached
./gm_memcached.sh stop    #停止memcached
./gm_memcached.sh restart #重启memcached

转载于:https://www.cnblogs.com/fdipzone/archive/2013/04/14/3715126.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值