如何使用给定模式尾部-f最新日志文件 [英] How to tail -f the latest log file with a given pattern
问题描述
我使用某些每小时生成一个日志文件的日志系统,如下所示:
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
我正在尝试跟踪最新的日志文件,并提供一种模式(例如SoftwareLog *),但我意识到有:
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.
有什么建议吗?
推荐答案
您可能想尝试多重尾巴- http://www.vanheusden.com/multitail/
You might want to try out multitail - http://www.vanheusden.com/multitail/
如果您想坚持Dennis Williamson的回答(我已经对他进行+1了),这是为您填充的空白.
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.
在您的shell中,运行以下脚本(或者等效于zsh,在看到zsh标记之前,我先用bash编写了该脚本):
In your shell, run the following script (or it's zsh equivalent, I whipped this up in bash before I saw the zsh tag):
#!/bin/bash
TARGET_DIR="some/logfiles/"
SYMLINK_FILE="SoftwareLog.latest"
SYMLINK_PATH="$TARGET_DIR/$SYMLINK_FILE"
function getLastModifiedFile {
echo $(ls -t "$TARGET_DIR" | grep -v "$SYMLINK_FILE" | head -1)
}
function getCurrentlySymlinkedFile {
if [[ -h $SYMLINK_PATH ]]
then
echo $(ls -l $SYMLINK_PATH | awk '{print $NF}')
else
echo ""
fi
}
symlinkedFile=$(getCurrentlySymlinkedFile)
while true
do
sleep 10
lastModified=$(getLastModifiedFile)
if [[ $symlinkedFile != $lastModified ]]
then
ln -nsf $lastModified $SYMLINK_PATH
symlinkedFile=$lastModified
fi
done
使用常规方法处理该背景(再次,我不知道zsh,所以可能有所不同)...
Background that process using the normal method (again, I don't know zsh, so it might be different)...
./updateSymlink.sh 2>&1 > /dev/null
然后单击tail -F $SYMLINK_PATH
,以便尾部指示符号链接的更改或文件的旋转.
Then tail -F $SYMLINK_PATH
so that the tail hands the changing of the symbolic link or a rotation of the file.
这有点令人费解,但是我不知道用尾巴做这件事的另一种方法.如果其他人知道可以处理此问题的实用程序,请让他们向前走,因为我也很想亲自查看它-默认情况下,如Jetty之类的应用程序会以这种方式记录日志,并且我总是编写在cron上运行的符号链接脚本来补偿为此.
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.
这篇关于如何使用给定模式尾部-f最新日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!