Shell脚本 - 日志文件未更新

时间:2016-10-25 02:27:57

标签: bash shell sh

如果2个日志文件没有更新,我试图让我的shell脚本提醒我。我正在尝试获取当前大小...等待10秒,如果文件没有更改大小,请发送电子邮件警报。任何帮助都会很棒!

我的代码:

#! /bin/bash
logFiles="lg.log lg2.log"
logLocation="/user/file/logs"
sleepTime=10
subject="issue - file not updated"

failMessage="::no recent updates "
successMessage="OK"
not_updated_time=0
arr=($logFiles)
arrlen=${#arr[@]}
arrcount=()
date
for ((count=0; count<arrlen; count++)) ; do
    arrcount[$count]=`ls -l  $logLocation${arr[$count]}  |awk '{print $5}'`
    echo "${arr[$count]} Original size :: ${arrcount[$count]}"
done
echo
while [ "e" == "e" ] ; do
    sleep $sleepTime
    date
    for ((count=0; count<arrlen; count++)) ; do
        nc=`ls -l  $logLocation${arr[$count]}  |awk '{print $5}'`
        echo -n "${arr[$count]} "
        if [ $nc == ${arrcount[$count]} ] ; then
        echo 'error' | mailx -s "issue" cat@gmail.com
        else
            arrcount[$count]=$nc
            echo $successMessage
   not_updated_time=0
        fi
    done
    echo
done

修复了逗号的问题,脚本仍无效。任何想法?

1 个答案:

答案 0 :(得分:0)

/的初始化或当您将该变量用作logLocation时,似乎缺少$logLocation${arr[$count]}

尝试以下两项更改之一:

logLocation="/user/file/logs/"

或者:

`ls -l  $logLocation/${arr[$count]}  |awk '{print $5}'`