Linux下,DIY apache和memcache守護進程

端午節,睡夢中,被電話驚醒,web服務器掛了,惱火壞了。登陸服務器一陣忙活,發現apache服務器和memcache服務器非法關閉了。哎,爲了睡得安穩,抓緊寫個小腳本來監控下appach和memcache吧!很快就有了下面的腳本:


#!/bin/bash -

name=`basename $0 .sh`
function showHelp(){
        echo "Usage: $name"
        echo "       $name test|dev"
        exit 1
}
if [ $# -ne 0 -a $# -ne 1 ]; then
        showHelp
fi
#正式/測試環境
test=0

if [ $# -eq 1 ]; then
#       echo "$1"
        if [ "$1" = "dev"  -o "$1" = "test" ]; then
                test=1
        else
                showHelp
        fi
fi

#echo $test

websrvkey=/bin/httpd
websrvbin=/etc/init.d/apachectl
cachekey=/memcached
cachebin=/usr/local/memcached/bin/memcached
cachepid=/usr/local/memcached/memcached.pid
cachedir=/usr/local/memcached


#run web server
function startweb(){
        echo "`date` start websrv..."
        if [ ! -f $websrvbin ]; then
                echo "`date` websrv bin [$websrvbin] not exist..."
                return
        fi
        $websrvbin start
        echo "`date` start websrv complete..."
}


#run cache
function startcache(){
        echo "`date` start cache..."

        if [ $test -eq 1 ]; then
                cachebin=/usr/bin/memcached
        fi

        if [ ! -f $cachebin ]; then
                echo "`date` cache bin [$cachebin] not exist..."
                return
        fi

        if [ ! -d $cachedir ]; then
                echo "`date` cache dir  not exist..."
                mkdir $cachedir
                echo "`date` create dir $cachedir"
        fi

        if [ $test -eq 0 ]; then
                $cachebin -d -m 100 -uroot -l 0.0.0.0 -p 11000 -c 512 -P $cachepid
        else
                $cachebin -d -m 128 -l 192.168.119.60 -p 12000 -u ossh
        fi
        echo "`date` start cache complete..."
}
cnt=`ps -ef | grep $websrvkey | grep -vc grep`
# echo $cnt
if [ $cnt -le 0 ]; then
        startweb
fi
cnt=`ps -ef | grep $cachekey | grep -vc grep`
#echo $cnt
if [ $cnt -le 0  ]; then
        startcache
fi


把上面的腳本保存爲monitorWebSrv.sh,並對該文件賦予可執行權限,進行如下操作:

chmod +x  /etc/app/slightphp/public/csevent/bin/crontab/monitorWebSrv.sh

然後加入crontab計劃任務,如下:

* * * * * /etc/app/slightphp/public/csevent/bin/crontab/monitorWebSrv.sh >> /tmp/monitorWebSrv.log
* * * * * sleep 30; /etc/app/slightphp/public/csevent/bin/crontab/monitorWebSrv.sh >> /tmp/monitorWebSrv.log

       爲什麼加兩條呢?爲了每隔30秒執行一次!

對於shell和crontab,請參閱以前的blog:shell腳本比較運算符及邏輯運算符小結 、Linux Shell腳本邏輯操作符簡介 及Linux crontab命令小結





發佈了248 篇原創文章 · 獲贊 141 · 訪問量 196萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章