月下博客

一个检测程序nan的脚本

程序中出现nan意味着崩溃,不对其进行判断则程序继续运行,浪费cpu。而如果程序中每次都检测,代价太大。折中之下另外写了一个检测脚本,既能及时发现程序崩溃,又无需原来的程序做额外操作,保证了性能。

脚本原理:

  1. 使用重定向、tee等将标准输出写入日志文件;
  2. 脚本定时(10s)用tail查看最新输出,发现nan则杀死程序,脚本退出。

脚本用法:

  1. 执行程序,将屏幕输出写入文件。例如:nohup ./ttt > ~/log.txt 2>&1 &,或用tee重定向:./ttt | tee log.txt
  2. ps命令找到程序的pid: ps aux | grep ttt | grep -v grep | awk '{print $2}'
  3. 执行监控:./checkNAN.sh pid log.txt

脚本内容:

#!/bin/bash
# author: tlanyan
# link: <https://ssrvps.org/archives/4030>
set -e

usage() {
    echo "Usage: ./checkNAN pid logfile"
}

argc=$#
if [ $argc -lt 2 ]
then
    usage
    exit 1
fi

PID=$1
LOGFILE=$2

COMMAND=`ps -ef | grep $PID | grep -v grep | grep -v checkNAN| head -n 1 | awk '{print $8}'`
if [ "$COMMAND" = "" ]; then
    echo "unknow pid: $PID"
    exit 1
fi

if [ ! -e "$LOGFILE" ]; then
    echo "non-exists log file: $LOGFILE"
    exit 1
fi

echo "watch pid: $PID($COMMAND) for log file: $LOGFILE"

count=0
while true
do
    ret=`ps -ef | grep $PID | grep -v grep | grep -v checkNAN| head -n 1 | awk '{print $8}'`
    if [ "$ret" = "" ]; then
        echo "process quit!"
        exit 0
    fi
    ret=$(tail $LOGFILE | grep -i nan|wc -l)
    if [[ $ret -ne 0 ]]; then
        echo "nan checked!"
        tail $LOGFILE | grep nan
        echo "kill process"
        kill -9 $PID
        echo "watch exit"
        exit 0
    fi

    count=$((count+1))
    if [[ $(($count%6)) -eq 0 ]]; then
        date=$(date +'%Y-%m-%d %H-%M-%S')
        echo "$date: no nan checked..."
    fi

    sleep 10
done