【发布时间】:2015-05-20 06:33:08
【问题描述】:
我想拖尾一个将不间断写入的日志文件,问题是在我的脚本中我不想指定一些行或字节等。拖尾,我想在我的脚本中指定每次尾随之前没有尾随的最后几行。我怎么能在我的脚本中做到这一点? 谢谢
【问题讨论】:
我想拖尾一个将不间断写入的日志文件,问题是在我的脚本中我不想指定一些行或字节等。拖尾,我想在我的脚本中指定每次尾随之前没有尾随的最后几行。我怎么能在我的脚本中做到这一点? 谢谢
【问题讨论】:
我认为 2010 年的这个答案基本上是要走的路,虽然你没有指定你正在使用的外壳:How to tail -f the latest log file with a given pattern
本质上,这个想法是保存最后一次拖尾文件的 sn-p,并使用该 sn-p 在文件的下一个尾部输出该 sn-p 之后的所有内容。
【讨论】:
我终于这样处理了:
#!/bin/bash
###Getting the file size (bytes)
end_of_file=`ls -la /root/mbox | cut -d ' ' -f5`
while sleep 1
do
### tail from previous end_of_file to the end
tout_finder=`tail -c+$end_of_file /root/mbox | grep -q "desired words" ; echo $?`
if [ $tout_finder == "0" ]; then
//mycode
###getting new size of file
end_of_file_new=`ls -la /root/mbox | cut -d ' ' -f5`
###replacing it with old one
end_of_file=$end_of_file_new
else
//mycode
###getting new size of file
end_of_file_new=`ls -la /root/mbox | cut -d ' ' -f5`
###replacing it with old one
end_of_file=$end_of_file_new
fi
done
【讨论】: