1,memcached_inc.sh 设置路径,端口等讯息。
代码:
2,gm_memcached.sh 控制memcached 启动,停止,重启。
#!/bin/sh
# site: www.jb200.com
#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 runningn"
else
printf "starting memcachedn"
$MEMCACHED
sleep 2
PID=$(cat $MEMCACHED_PID_FILE)
printf "memcached is started PID:$PIDn"
printf "starting memcached daemonn"
${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 stoppedn"
else
printf "no memcached daemon runningn"
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 stoppedn"
else
printf "no memcached runningn"
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 进程,如进程失效则自动启动。
代码:
调用方法: