bash 尾料轧制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8647690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tailing Rolling Files
提问by user1118047
I have a directory full of rolling log files that I would like to be able to use tail on.
我有一个充满滚动日志文件的目录,我希望能够在其上使用 tail。
The files are named as such:
这些文件被命名为:
name modified
00A.txt Dec 27 19:00
00B.txt Dec 27 19:01
00C.txt Dec 27 19:02
00D.txt Dec 27 19:03
On an older unix system, I'm trying to come up with a shell script that will tail the most recently modified file in a specific directory, and if that file gets administratively closed (rolls to the next file) I want the program to automatically begin tailing the new file without me having to break out of tail to rerun.
在较旧的 unix 系统上,我试图想出一个 shell 脚本,该脚本将跟踪特定目录中最近修改的文件,如果该文件被管理关闭(滚动到下一个文件),我希望程序自动开始拖尾新文件,而我不必打破尾部重新运行。
tail -100f `ls -t | head -1`
The desired behavior, given the filenames above, would go like this:
给定上面的文件名,所需的行为将如下所示:
./logtailer.sh
Then the script would begin tailing 00D.txt. As soon as the logger was finished writing to 00D.txt and the newest log file was now named 00E.txt, the program would automatically begin tailing that file.
然后脚本将开始拖尾 00D.txt。一旦记录器完成写入 00D.txt 并且最新的日志文件现在被命名为 00E.txt,程序将自动开始拖尾该文件。
One could write this script by watching the output of tail for the text "File Administratively Closed" and then having the following command run again.
可以通过观察 tail 输出文本“文件管理关闭”然后再次运行以下命令来编写此脚本。
tail -100f `ls -t | head -1`
Is there a more elegant way to do this than by watching for the text "file administratively closed"? How can I even read the output of tail line by line in a shell script?
有没有比通过查看“文件管理关闭”文本更优雅的方法来做到这一点?我什至如何在 shell 脚本中逐行读取 tail 的输出?
Edit: I should explain that the -F flag for tail is not an option for me on this system. It uses a different version of tail that does not contain this feature. OS version - Solaris 10
编辑:我应该解释一下,tail 的 -F 标志不是我在这个系统上的一个选项。它使用不包含此功能的不同版本的 tail。操作系统版本 - Solaris 10
回答by jaypal singh
You can use the -Foption for tailwhich implies --follow=name --retry.
您可以使用其中隐含的-F选项。tail--follow=name --retry
From the manpage:
从man页面:
-F
The -F option implies the -f option, but tail will also check to see if the
file being followed has been renamed or rotated. The file is closed and
reopened when tail detects that the filename being read from has a new inode
number. The -F option is ignored if reading from standard input rather than
a file.
回答by Samus_
you may need inotifyto detect the creation of the new file, a workaround for that would be to keep polling the filesystem while running tail on the background:
您可能需要inotify检测新文件的创建,解决方法是在后台运行 tail 时继续轮询文件系统:
#!/bin/bash
get_latest() {
local files=(*.log)
local latest=${files[${#files[@]}-1]}
echo "$latest"
[[ $latest == ]]
}
run_tail() {
tail -c +0 -f ""
}
while true; do
while current=$(get_latest "$current"); do
sleep 1
done
[[ $pid ]] && kill $pid
run_tail "$current" & pid=$!
done
(untested, unnecesarily hacky and be careful with the limitations of your old system!)
(未经测试,不必要的hacky,请注意旧系统的局限性!)

