本文介绍了对该bash脚本的改进以模拟& quot; tail --follow& amp; amp; amp; quot;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要远程尾部日志文件,以便即使将文件翻转也可以继续进行尾部工作.

I need to remote tail log files such that the tailing continues working even when the file is rolled over.

我这样做的尝试是通过直接在ssh上使用tail命令开始的:

My attempts to do so, started by directly using the tail command over ssh:

ssh root@some-remote-host tail -1000f /some-directory/application.log | tee /c/local-directory/applicaiton.log

这使我可以使用Otroslogviewer在本地过滤/c/local-directory/applicaiton.log (这是试图在本地尾随日志文件的原始目的).

That allows me to filter through /c/local-directory/applicaiton.log locally using Otroslogviewer (which was the original purpose of trying to tail the log file locally).

当远程日志文件翻转时(每10MB发生一次),以上内容不再尾部.我没有更改翻转设置所需的访问权限.

The above stops tailing when the remote log file is rolled over (which happens at every 10MB). I do not have the access required to change the roll over settings.

不幸的是,远程操作系统(Solaris)上的 tail 版本都不支持-follow (或 -f )选项,该选项可以可以处理文件翻转,因此我不得不编写以下 tailer.sh 脚本来模拟该选项:

Unfortunately, none of the tail versions on the remote OS (Solaris) support a --follow (or -f) option which can handle file rollovers, so I had to write the following tailer.sh script to simulate that option:

<!-- language: lang-bash -->
#!/bin/bash

function startTail {
echo "Path: $1"
tail -199999f "$1" 2>/dev/null &                 #Try to tail the file
while [[ -f $1 ]]; do sleep 1; done              #Check every second if the file still exists
echo "***** File Not Available as of: $(date)"   #If not then log a message and,
kill "$!" 2>/dev/null                            #Kill the tail process, then


echo "Waiting for file to appear"                #Wait for the file to re-appear
while [ ! -f "$1" ]
do
  echo -ne  "."                                  #Show progress bar
  sleep 1
done

echo -ne '\n' #Advance to next line              #File has appeared again
echo "Starting Tail again"

startTail "$1"
}

startTail "$1"

我对上述脚本比较满意.但是,由于远程操作系统上 sleep 命令的局限性,它存在一个问题.它只能接受整数,因此 sleep 1 是我在再次检查文件是否存在之前可以等待的最短时间.该时间段有时足以检测文件翻转,但失败次数却不足以成为我要解决的问题.

I am relatively happy with the above script. However, it suffers from one issue stemming from the limitation of the sleep command on the remote OS. It can only accept whole numbers, so sleep 1 is the smallest amount of time I can wait before checking for the existence of the file again. That time period is enough to detect a file rollover sometimes, but fails enough number of times to be a problem I want to fix.

我唯一想到的另一种方法是通过检查文件大小来实现文件翻转检查.因此,请每隔一秒钟检查一次文件大小,如果它小于先前记录的大小小于,则文件将被翻转.然后,重新开始尾巴.

The only other way I can think of is to implement a file-rollover check by checking for the file size. So, check for the filesize every one second, if it's less than the previously recorded size then the file was rolled over. Then, re-start the tail.

我检查了其他更可靠的替代方法,例如inotifywait,inotify,但它们在远程服务器上不可用,并且我无权安装它们.

I checked for other more reliable alternatives like inotifywait, inotify but they are not available on the remote server(s) and I do not have the access to install them.

您能想到其他方法来使用bash脚本检测文件翻转吗?

Can you think of any other way to detect a file rollover with a bash script?

基于下面赫玛的回答,修改后的(有效!)脚本如下:

Based on Hema's answer below, the modified (working!) script is as follows:

#!/bin/bash


function startTail {
echo "Path: $1"
tail -199999f "$1" 2>/dev/null &            #Try to tail the file

#Check every second if the file still exists
while [[ -f $1 ]]
do
perl -MTime::HiRes -e "Time::HiRes::sleep(0.1)"
done

echo "***** File Not Available as of: $(date)"  #If not then log a message and,
kill $! 2>/dev/null             #Kill the tail process, then


echo "Waiting for file to appear"       #Wait for the file to re-appear
while [ ! -f $1 ]
do
  echo -ne  "."                 #Show progress bar
  sleep 1
done

echo -ne '\n' #Advance to next line     #File has appeared again
echo "Starting Tail again"

startTail "$1"
}

startTail "$1"

推荐答案

要睡几微秒,您可以使用

For sleeping in microseconds, you can use

perl -MTime::HiRes -e "Time::HiRes::usleep(1)" ;
perl -MTime::HiRes -e "Time::HiRes::sleep(0.001)" ;

这篇关于对该bash脚本的改进以模拟&amp; quot; tail --follow&amp; amp; amp; amp; quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 08:03