shell监控程序内存以及程序运行状态并写入日志

KVDBDataBaseImsiRegister为监控程序的名称

LOG_NAME为日志文件名称

#!/bin/bash

LOG_PREFIX="../log/KVDBDataBaseImsiRegister_"
DATE_TIME=`date +%Y%m%d`
LOG_SUFFIX=".log"
LOG_NAME=${LOG_PREFIX}${DATE_TIME}${LOG_SUFFIX}

while [ 1=1 ]
do
        cd /home/cattsoft/GX_UselessCard_Task/KVDBDataBaseImsiRegister/bin
        MEMS=`ps aux | grep KVDBDataBaseImsiRegister | grep -v grep | grep -v KVDBDataBaseImsiRegister_watch.sh |awk '{print $4}'`
        PIDS=`ps aux | grep KVDBDataBaseImsiRegister | grep -v grep | grep -v KVDBDataBaseImsiRegister_watch.sh | awk '{print $2}'`
        #写日志
        curTM=`date +%Y-%m-%d `/`date +%H:%M:%S`
        
        #进程关闭
        if [ -z "$PIDS" ]
        then
                echo "[$curTM]  Not exsist!"
                `echo $curTM">[ERROR]: KVDBDataBaseImsiRegister is Not exsist! KVDBDataBaseImsiRegister is rebooting" >> $LOG_NAME`
                sh start.sh
        elif [ `echo ""$MEMS" > 50.0" | bc` = 1 ]
        then
                echo "[$curTM]  the memory over limit!!!"
                `echo $curTM ">[INFO]:  the memory over limit!!!" >> $LOG_NAME`
                kill -9 ${PIDS}
                #sh start.sh
        else
                 echo "[$curTM]  Normal"
                `echo $curTM">[INFO]: KVDBDataBaseImsiRegister is Normal" >> $LOG_NAME`
        fi
        sleep  1
done
 

脚本为了防止终端关闭程序退出,需要放到后台运行

假设脚本名称为 KVDBDataBaseImsiRegister_watch.sh

后台运行脚本命令

nohup ./KVDBDataBaseImsiRegister_watch.sh &

 

 

你可能感兴趣的:(shell编程,shell)