如何使用给定模式 tail -f 最新日志文件
我使用一些日志系统,它每小时创建一个日志文件,如下所示:
SoftwareLog.2010-08-01-08
SoftwareLog.2010-08-01-09
SoftwareLog.2010-08-01-10
我试图追踪最新的日志文件,给出一种模式(例如 SoftwareLog*),我意识到有:
tail -F (tail --follow=name --retry)
但只遵循一个特定名称 -这些按日期和时间有不同的名称。我尝试过类似的操作:
tail --follow=name --retry SoftwareLog*(.om[1])
但通配符语句在传递给 tail 之前已被解析,并且不会在每次 tail 重试时重新执行。
有什么建议吗?
I work with some log system which creates a log file every hour, like follows:
SoftwareLog.2010-08-01-08
SoftwareLog.2010-08-01-09
SoftwareLog.2010-08-01-10
I'm trying to tail to follow the latest log file giving a pattern (e.g. SoftwareLog*) and I realize there's:
tail -F (tail --follow=name --retry)
but that only follow one specific name - and these have different names by date and hour. I tried something like:
tail --follow=name --retry SoftwareLog*(.om[1])
but the wildcard statement is resoved before it gets passed to tail and doesn't re-execute everytime tail retries.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我相信最简单的解决方案如下:
现在,如果您的目录包含其他日志文件,例如“SystemLog”,并且您只需要最新的“SoftwareLog”文件,那么您只需包含一个 grep 即可,如下所示:
I believe the simplest solution is as follows:
Now, if your directory contains other log files like "SystemLog" and you only want the latest "SoftwareLog" file, then you would simply include a grep as follows:
[编辑:在快速搜索工具后]
您可能想尝试 multitail - http://www. vanheusden.com/multitail/
如果您想坚持丹尼斯·威廉姆森的答案(我已经相应地为他+1),这里是为您填写的空白。
在你的 shell 中,运行以下脚本(或者它是 zsh 的等效脚本,我在看到 zsh 标签之前在 bash 中启动了它):
使用正常方法后台该进程(同样,我不知道 zsh,所以它可能会有所不同) )...
./updateSymlink.sh 2>&1> /dev/null
然后
tail -F $SYMLINK_PATH
以便尾部负责符号链接的更改或文件的旋转。这有点令人费解,但我不知道还有另一种方法可以用尾巴做到这一点。如果其他人知道处理此问题的实用程序,那么让他们继续前进,因为我自己也很想看到它 - 像 Jetty 这样的应用程序默认情况下会以这种方式记录,并且我总是编写一个在 cron 上运行的符号链接脚本来补偿为了它。
[编辑:从其中一行的末尾删除了错误的“j”。您还有一个错误的变量名称“lastModifiedFile”不存在,您设置的正确名称是“lastModified”]
[Edit: after a quick googling for a tool]
You might want to try out multitail - http://www.vanheusden.com/multitail/
If you want to stick with Dennis Williamson's answer (and I've +1'ed him accordingly) here are the blanks filled in for you.
In your shell, run the following script (or it's zsh equivalent, I whipped this up in bash before I saw the zsh tag):
Background that process using the normal method (again, I don't know zsh, so it might be different)...
./updateSymlink.sh 2>&1 > /dev/null
Then
tail -F $SYMLINK_PATH
so that the tail hands the changing of the symbolic link or a rotation of the file.This is slightly convoluted, but I don't know of another way to do this with tail. If anyone else knows of a utility that handles this, then let them step forward because I'd love to see it myself too - applications like Jetty by default do logs this way and I always script up a symlinking script run on a cron to compensate for it.
[Edit: Removed an erroneous 'j' from the end of one of the lines. You also had a bad variable name "lastModifiedFile" didn't exist, the proper name that you set is "lastModified"]
我还没有对此进行测试,但可能有效的方法是运行一个后台进程,该进程创建并更新到最新日志文件的符号链接,然后您将
tail -f
(或tail -F
) 符号链接。I haven't tested this, but an approach that may work would be to run a background process that creates and updates a symlink to the latest log file and then you would
tail -f
(ortail -F
) the symlink.然后运行它:
tail.sh 'SoftwareLog*'
如果在检查之间写入日志,脚本将丢失一些日志行。但至少它是一个脚本,不需要符号链接。
Then run it as:
tail.sh 'SoftwareLog*'
The script will lose some log lines if the logs are written to between checks. But at least it's a single script, with no symlinks required.
我们每天轮换日志文件为:
/var/log/grails/customer-2020-01-03.log
。为了tail
最新的一个,以下命令对我来说效果很好:(注意:表达式中的
+
符号后面没有空格)所以,对于您来说,以下内容应该有效(如果您位于日志的同一目录中):
We have daily rotating log files as:
/var/log/grails/customer-2020-01-03.log
. Totail
the latest one, the following command worked fine for me:(NOTE: no space after the
+
sign in the expression)So, for you, the following should work (if you are in the same directory of the logs):
我相信最简单的方法是将 tail 与 ls 和 head 一起使用,尝试这样的方法
I believe the easiest way is to use tail with ls and head, try something like this