自動檢測進程腳本

#!/bin/bash
#######################
##modify by liugj
#at may 3 2011
#######################
#cp this script to /usr/local/monitor/

####take care this $PATH
. /etc/profile

p_name="tmss"
log=/home/liugj/moniter_logs/tmss/$p_name.log

####check the catalog and logfile
if [ ! -d $(dirname $log) ]
then
        mkdir -p $(dirname $log)
        touch $log
else
        if [ ! -e $log ];then
                touch  $log
        fi
fi

####clean up the log
line=`cat $log|wc -l`
if [ $line -gt 20000 ]
then
                > $log
fi

####get ip
IP=`ifconfig|grep "inet addr"|grep -v "127.0.0.1" |awk '{print $2}'|awk -F: '{ORS=" ";print $2}'|awk '{print $1}'`

####judge the ria-run process
num=`netstat -antp|grep  '\<9900\>'|wc -l`

if [ "$num" -gt "0" ]
then
        echo "good";
        log_good=$(date +%F"_"%T)"_"$p_name"_good_"$IP
        ##write log
        echo $log_good  >> $log
        exit;
else
        log_bad=$(date +%F"_"%T)"_"$p_name"_down_"$IP

        ##write log
        echo $log_bad  >> $log

        #restart the process
        ##################
        cd /home/TMSS

        ./tvs_gd_wht61 &

        ###################

        ####wait 10 sec
        sleep 10

        num=`netstat -antp|grep  '\<9900\>'|wc -l`

                if [ $num -gt 0 ]
                then
                        log_body=$p_name"_"$IP"restarted_sucessfull_"$(date +%F"_"%T)
                        echo $log_body >> $log;

                else
                        log_body=$p_name"_"$IP"restarted_failed_"$(date +%F"_"%T)
                        echo $log_body >> $log;
                fi


fi
 ### */1 * * * * /home/liugj/moniter_3gss
 ####chmod +x moniter_3gss

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章