リアルタイムログアナライザ - 数秒ごとにファイルアクセス

StackOverflow https://stackoverflow.com//questions/22011879

  •  21-12-2019
  •  | 
  •  

質問

私はBashで簡単なスクリプトを書いています。リアルタイムでいくつかのログを分析し、事実に近づく方法について疑問に思います。 今私はこのようなことをしています:

LOG_FILE=path_to_file
DELAY=1   #time between refresh
LINES=100 #lines to read at one cycle

LAST=$(tail -n 1 $LOG_FILE)      

IFS=$'\n'
while true;
do
    clear;
    found=0
    LOG=$(tail -n $LINES $LOG_FILE)
    for line in $LOG
    do
        if [ $line = $LAST ]; then 
            found=1
            continue
        fi        
        if [ $found = 0 ]; then
            continue
        fi
        #Analyzing counting nd stuff.
        echo "$stuff"
    done
    LAST=$line
    sleep $DELAY;
done
.

だからすべてのサイクルは、ファイルの終わりから数回の行を取得し、前回の実行の最後のものを探しています。これは、定義された数の行数が追加されるように、1サイクルよりも非常に大丈夫です。私は常にLINES=10000のようなものを言うことができますが、この場合、私が以前の実行から最後の行を見つけたかどうかを判断するためだけに無駄なランの塚があるでしょう。 私はやや効率的なことができるかどうか疑問に思いますか?

役に立ちましたか?

解決

あなたはこのようなsthを探していると思います:

#!/bin/bash
GAP=10     #How long to wait
LOGFILE=$1 #File to log to

if [ "$#" -ne "1" ]; then
    echo "USAGE: `basename $0` <file with absolute path>"
    exit 1
fi


#Get current long of the file
len=`wc -l $LOGFILE | awk '{ print $1 }'`
echo "Current size is $len lines."

while :
do
    if [ -N $LOGFILE ]; then
        echo "`date`: New Entries in $LOGFILE: "
        newlen=`wc -l $LOGFILE | awk ' { print $1 }'`
        newlines=`expr $newlen - $len`
        tail -$newlines $LOGFILE
        len=$newlen
    fi
sleep $GAP
done
exit 0
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top