改进这个bash脚本模拟"尾--follow" [英] Improvements to this bash script to simulate "tail --follow"

查看:188
本文介绍了改进这个bash脚本模拟"尾--follow"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要远程跟踪日志文件,使得尾矿继续工作,即使文件被辗过。

我尝试这样做,通过直接使用tail命令通过SSH启动:

  SSH根@一些远程主机尾-1000f /some-directory/application.log |三通/c/local-directory/applicaiton.log

这让我通过 /c/local-directory/applicaiton.log 过滤本地使用Otroslogviewer(这是试图尾日志文件的初衷本地)

以上停止拖尾当远程日志文件翻了个身(这在每一个10MB的发生)。我没有改变过设置在辊所需的访问

不幸的是,没有远程操作系统(Solaris)上的版本支持 - 遵循(或 -f )的选项,它可以处理文件翻车,所以我不得不写以下 tailer.sh 脚本来模拟选项​​:

 <  - 语言:郎庆典 - >
#!/斌/庆典功能startTail {
回声路径:$ 1
尾-199999f$ 12 - ;的/ dev / null的&安培; #Try尾文件
而[[-f $ 1]];做睡1; #检查做每一秒,如果该文件仍然存在
回声*****文件中没有提供为:$(日期)#如果没有则记录消息,并
杀$! 2 - ;的/ dev / null的#Kill尾进程,那么
回声等待文件出现#WAIT的文件重新出现
而[! -f$ 1]

  回声-ne。 #Show进度条
  睡眠1
DONE回声-ne的'\\ n'#Advance到下一行#FILE又出现
回声再次启动尾巴startTail$ 1
}startTail$ 1

我上面的脚本比较高兴。然而,它从一个问题从睡眠命令远程操作系统上的限制所产生的困扰。它只能接受整数,所以睡眠1 是我能再次为文件是否存在前检查等待​​的时间量最小。那段时间是足够的,有时检测文件滚动更新,但无法次足够多的是我要解决问题。

我能想到的唯一的另一种方式是通过检查文件的大小来实现文件的翻转检查。因此,检查文件大小每一秒,如果它的超过previously记录大小,然后将文件滑过。然后,重新启动的尾巴。

我检查像inotifywait其他更可靠的替代品,但的inotify他们不提供远程服务器(S)上我没有安装它们的机会。

你能想到的任何其他方式来检测一个bash脚本文件翻转?


编辑:基于下面喝骂的答案,修改后的(!工作)脚本如下:

 #!/斌/庆典
功能startTail {
回声路径:$ 1
尾-199999f$ 12 - ;的/ dev / null的&安培; #Try尾文件#检查每一秒,如果该文件仍然存在
而[[-f $ 1]]

perl的-mtime ::高分辨率-e时间::高分辨率::睡眠(0.1)
DONE回声*****文件中没有提供为:$(日期)#如果没有则记录消息,并
杀$! 2 - ;的/ dev / null的#Kill尾进程,那么
回声等待文件出现#WAIT的文件重新出现
而[! -f $ 1]

  回声-ne。 #Show进度条
  睡眠1
DONE回声-ne的'\\ n'#Advance到下一行#FILE又出现
回声再次启动尾巴startTail$ 1
}startTail$ 1


解决方案

有关微秒睡觉,你可以使用

  perl的-mtime ::高分辨率-e时间::高分辨率:: usleep(1);
perl的-mtime ::高分辨率-e时间::高分辨率::睡眠(0.001);

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

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

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).

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.

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"

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.

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.

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


Edit: 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脚本模拟&QUOT;尾--follow&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆