如何使用给定模式 tail -f 最新日志文件

发布于 2024-09-13 01:23:32 字数 445 浏览 6 评论 0原文

我使用一些日志系统,它每小时创建一个日志文件,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

孤云独去闲 2024-09-20 01:23:32

我相信最简单的解决方案如下:

tail -f `ls -tr | tail -n 1`

现在,如果您的目录包含其他日志文件,例如“SystemLog”,并且您只需要最新的“SoftwareLog”文件,那么您只需包含一个 grep 即可,如下所示:

tail -f `ls -tr | grep SoftwareLog | tail -n 1`

I believe the simplest solution is as follows:

tail -f `ls -tr | tail -n 1`

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:

tail -f `ls -tr | grep SoftwareLog | tail -n 1`
女中豪杰 2024-09-20 01:23:32

[编辑:在快速搜索工具后]

您可能想尝试 multitail - http://www. vanheusden.com/multitail/

如果您想坚持丹尼斯·威廉姆森的答案(我已经相应地为他+1),这里是为您填写的空白。

在你的 shell 中,运行以下脚本(或者它是 zsh 的等效脚本,我在看到 zsh 标签之前在 bash 中启动了它):

#!/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,所以它可能会有所不同) )...

./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):

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

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"]

倾其所爱 2024-09-20 01:23:32

我还没有对此进行测试,但可能有效的方法是运行一个后台进程,该进程创建并更新到最新日志文件的符号链接,然后您将 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 (or tail -F) the symlink.

岁月流歌 2024-09-20 01:23:32
#!/bin/bash

PATTERN="$1"

# Try to make sure sub-shells exit when we do.
trap "kill -9 -- -$BASHPID" SIGINT SIGTERM EXIT

PID=0
OLD_FILES=""
while true; do
  FILES="$(echo $PATTERN)"
  if test "$FILES" != "$OLD_FILES"; then
    if test "$PID" != "0"; then
      kill $PID
      PID=0
    fi
    if test "$FILES" != "$PATTERN" || test -f "$PATTERN"; then
      tail --pid=$ -n 0 -F $PATTERN &
      PID=$!
    fi
  fi
  OLD_FILES="$FILES"
  sleep 1
done

然后运行它: tail.sh 'SoftwareLog*'

如果在检查之间写入日志,脚本将丢失一些日志行。但至少它是一个脚本,不需要符号链接。

#!/bin/bash

PATTERN="$1"

# Try to make sure sub-shells exit when we do.
trap "kill -9 -- -$BASHPID" SIGINT SIGTERM EXIT

PID=0
OLD_FILES=""
while true; do
  FILES="$(echo $PATTERN)"
  if test "$FILES" != "$OLD_FILES"; then
    if test "$PID" != "0"; then
      kill $PID
      PID=0
    fi
    if test "$FILES" != "$PATTERN" || test -f "$PATTERN"; then
      tail --pid=$ -n 0 -F $PATTERN &
      PID=$!
    fi
  fi
  OLD_FILES="$FILES"
  sleep 1
done

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.

墨洒年华 2024-09-20 01:23:32

我们每天轮换日志文件为:/var/log/grails/customer-2020-01-03.log。为了 tail 最新的一个,以下命令对我来说效果很好:(

tail -f /var/log/grails/customer-`date +'%Y-%m-%d'`.log

注意:表达式中的 + 符号后面没有空格)

所以,对于您来说,以下内容应该有效(如果您位于日志的同一目录中):

tail -f SoftwareLog.`date +'%Y-%m-%d-%H'`

We have daily rotating log files as: /var/log/grails/customer-2020-01-03.log. To tail the latest one, the following command worked fine for me:

tail -f /var/log/grails/customer-`date +'%Y-%m-%d'`.log

(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 -f SoftwareLog.`date +'%Y-%m-%d-%H'`
-小熊_ 2024-09-20 01:23:32

我相信最简单的方法是将 taillshead 一起使用,尝试这样的方法

tail -f `ls -t SoftwareLog* | head -1`

I believe the easiest way is to use tail with ls and head, try something like this

tail -f `ls -t SoftwareLog* | head -1`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文