use*_*047 15 bash shell logging tail
我有一个充满滚动日志文件的目录,我希望能够使用tail.
这些文件的名称如下:
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
Run Code Online (Sandbox Code Playgroud)
在一个较旧的unix系统上,我试图想出一个shell脚本,它将在最近修改的特定目录中修改文件,如果该文件被管理关闭(滚动到下一个文件),我想让程序自动开始拖尾新文件,我不必打破尾巴重新运行.
tail -100f `ls -t | head -1`
Run Code Online (Sandbox Code Playgroud)
给定上面的文件名,所需的行为将如下所示:
./logtailer.sh
Run Code Online (Sandbox Code Playgroud)
然后脚本将开始拖尾00D.txt.一旦记录器完成写入00D.txt并且最新的日志文件现在命名为00E.txt,程序将自动开始拖尾该文件.
可以通过观察文件"文件管理已关闭"的尾部输出然后再次运行以下命令来编写此脚本.
tail -100f `ls -t | head -1`
Run Code Online (Sandbox Code Playgroud)
有没有比通过观察"文件管理关闭"文本更优雅的方式?我怎样才能在shell脚本中逐行读取尾部的输出?
编辑:我应该解释一下尾部的-F标志对我来说不是一个选项.它使用不同版本的尾部,不包含此功能.操作系统版本 - Solaris 10
jay*_*ngh 19
您可以使用暗示的-F
选项.tail
--follow=name --retry
从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.
Run Code Online (Sandbox Code Playgroud)