监控最新的日志文件的shell脚本
2014-11-29 01:22
531 查看
好长时间没写过博客了,快荒芜了,赶紧更新个。。。
在调试大的项目脚本的时候,有时候每次运行都会产生一个新的日志文件,然后还经常出错,就要每次去找相应的新的日志文件来看看那里出问题了,非常麻烦。。。
所以这个时候脚本就会很有用了,下面的脚本就是用来监控log目录中最后被修改的文件。
#! /bin/bash
help()
{
cat<<HELP
Usage:
-p file path; default path is "log/"
-h help
HELP
exit 0
}
filebase="log/"
while [ -n $1 ]
do
case $1 in
-h) help;;
-p) filebase=$2;shift 2;;
-*) echo "input error!"; exit 1;;
*) break;
esac
done
dir=$(ls $filebase)
num=${#dir[*]}
newestfile=${dir[0]}
i=1
while [ $i -lt $num ]
do
if [ "$filebase$newestfile" -ot "$filebase${dir[$i]}" ]
then newestfile=${dir[$i]}
fi
i=`expr $i + 1`
done
echo "the newest log is $newestfile"
tail -f $filebase$newestfile
测试脚本:
#! /bin/bash
touch ./log/test
firsttime=`date +%s`
while true
do
secondtime=`date +%s`
if [ $firsttime -lt $secondtime ]
then echo $secondtime >> ./log/test
firsttime=$secondtime
fi
done
在调试大的项目脚本的时候,有时候每次运行都会产生一个新的日志文件,然后还经常出错,就要每次去找相应的新的日志文件来看看那里出问题了,非常麻烦。。。
所以这个时候脚本就会很有用了,下面的脚本就是用来监控log目录中最后被修改的文件。
#! /bin/bash
help()
{
cat<<HELP
Usage:
-p file path; default path is "log/"
-h help
HELP
exit 0
}
filebase="log/"
while [ -n $1 ]
do
case $1 in
-h) help;;
-p) filebase=$2;shift 2;;
-*) echo "input error!"; exit 1;;
*) break;
esac
done
dir=$(ls $filebase)
num=${#dir[*]}
newestfile=${dir[0]}
i=1
while [ $i -lt $num ]
do
if [ "$filebase$newestfile" -ot "$filebase${dir[$i]}" ]
then newestfile=${dir[$i]}
fi
i=`expr $i + 1`
done
echo "the newest log is $newestfile"
tail -f $filebase$newestfile
测试脚本:
#! /bin/bash
touch ./log/test
firsttime=`date +%s`
while true
do
secondtime=`date +%s`
if [ $firsttime -lt $secondtime ]
then echo $secondtime >> ./log/test
firsttime=$secondtime
fi
done
相关文章推荐
- zabbix3.0.4通过自定义shell脚本添加对关键日志文件的监控
- zabbix3.0.4通过自定义shell脚本添加对关键日志文件的监控
- SQL脚本:监控当前重做日志文件使用情况
- 使用shell脚本进行服务器系统监控——文件系统监控(1)
- shell脚本清空日志文件
- 一个监控LINUX目录和文件变化的Shell脚本分享
- 使用RMAN和控制文件备份删除归档日志的SHELL脚本--RED HAT 5 LINUX 64
- shell脚本实现自动压缩一天前的日志文件并传到ftp服务器上
- shell脚本 操作日志文件 && 读取文件 && 查询进程ID
- shell脚本获取时间、crontab、日志重定向到文件
- [Oracle数据库监控] Oracle告警警日志文件、监听监控脚本
- 使用shell脚本进行服务器系统监控——文件系统监控(2)
- 使用shell脚本进行服务器系统监控——文件系统监控(5)
- 使用shell脚本进行服务器系统监控——文件系统监控(4)
- 利用shell脚本监控目录内文件改动
- SQL脚本:监控当前重做日志文件使用情况
- 监控数据库锁阻塞_机器连接数_警报日志的shell脚本 分享
- 使用shell脚本进行服务器系统监控——文件系统监控(5)
- 使用shell脚本进行服务器系统监控——文件系统监控(1)
- 使用RMAN和控制文件备份删除归档日志的SHELL脚本--RED HAT 5 LINUX 64