bash - 改进此 bash 脚本以模拟 "tail --follow"

标签 bash solaris sleep tail

我需要远程尾部日志文件,这样尾部即使在文件滚动时也能继续工作。

我尝试这样做,首先通过 ssh 直接使用 tail 命令:

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

这让我可以使用 Otroslogviewer 在本地过滤 /c/local-directory/applicaiton.log(这是尝试在本地跟踪日志文件的最初目的)。

当远程日志文件滚动时(每 10MB 发生一次),上面的代码停止拖尾。我没有更改翻转设置所需的访问权限。

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

<!-- 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 是我在再次检查文件是否存在之前可以等待的最短时间。该时间段有时足以检测到文件翻转,但失败次数足以成为我想要修复的问题。

我能想到的唯一其他方法是通过检查文件大小来实现文件翻转检查。因此,每隔一秒检查一次文件大小,如果它小于 之前记录的大小,则文件被翻转。然后,重新启动尾部。

我检查了其他更可靠的替代品,如 inotifywait、inotify,但它们在远程服务器上不可用,而且我无权安装它们。

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


编辑:根据下面盒马的回答,修改后的(有效!)脚本如下:

#!/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"

最佳答案

要以微秒为单位休眠,您可以使用

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

关于bash - 改进此 bash 脚本以模拟 "tail --follow",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21985069/

相关文章:

c - iconv_open() 在 Solaris 8 上返回 EINVAL

c - 未定义对 "sleep"的引用,但我确实包含了 <unistd.h>

C++ sleep() 中断程序

java - 使用 Java 程序运行 Bash 命令的处理输入

linux - 如果目标文件不存在,如何退出 shell 脚本?

python - 错误 : NameError: name 'subprocess' is not defined

c - 使用 sleep 后无法在定期间隔后显示输出

Bash 子 shell errexit 语义

Bash 输入变量和循环

c++ - EMF 文件(.so)调试,符号未找到 VTable 错误