Linux tail 使用 busybox 旋转日志文件

标签 linux bash logging busybox

在我的 bash 脚本中,我试图从/var/log/message 日志文件中监视输出 - 即使文件轮换(重新创建并重新启动)也继续。我尝试使用 tail -f filename 但很快意识到这对文件旋转没有好处。

因此,对于使用 tail -F filenametail -f --retry filename(以及其他一些变体)有很多答案。

但是在我的嵌入式 Linux 上,我使用的是 busybox,它有一个轻量级的 tail 版本:

tail [OPTIONS] [FILE]...

Print last 10 lines of each FILE to standard output. With more than one
FILE, precede each with a header giving the file name. With no FILE, or
when FILE is -, read standard input.

Options:
        -c N[kbm]       Output the last N bytes
        -n N[kbm]       Print last N lines instead of last 10
        -f              Output data as the file grows
        -q              Never output headers giving file names
        -s SEC          Wait SEC seconds between reads with -f
        -v              Always output headers giving file names

If the first character of N (bytes or lines) is a '+', output begins with
the Nth item from the start of each file, otherwise, print the last N items
in the file. N bytes may be suffixed by k (x1024), b (x512), or m (1024^2).

所以我不能执行通常的 tail -F ... 因为那个选项没有实现。上面的文档片段是最新的 busybox 版本——我的版本有点旧。

所以我需要另一种记录/var/log/messages 的方式,因为文件在一定大小时会被覆盖。

我在想一些简单的 bash 行。所以我看到了 inotifywait 之类的东西,但是 busybox 没有。我看这里:

busybox docs并且有一个 inotifyd,但我的版本没有那个特定的命令。所以我想知道是否有一种聪明的方法可以使用简单的 Linux 命令/命令组合来执行此操作,例如 watchtail -fcat/less/more 等...我无法完全弄清楚我需要用我拥有的有限命令做什么:(

最佳答案

日志是如何轮换的?您使用的是 logrotate 实用程序吗? 如果是,您是否尝试将您的行添加到配置文件中的 postrotate 部分?

来自 man logrotate

后旋转/结束脚本 postrotate 和 endscript 之间的线(两者都必须 单独出现在行上)在日志文件之后执行 被旋转。这些指令可能只出现在日志中 文件定义。另见预旋转。

关于Linux tail 使用 busybox 旋转日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45074111/

相关文章:

python - ImportError:Python 2.7.13 中没有名为 _tkinter 的模块

linux - GDB 附加到可执行文件被删除的进程

linux - 我可以使用 ECHO 来执行命令吗?

regex - 将单词括在单引号中的 Bash 脚本

python - 可用于日志格式的调用堆栈

spring - Spring boot 中特定包的自定义日志文件

linux - 将嵌套子目录中的文件复制到另一个文件夹中

bash - SH脚本根据文件名将文件从一个目录移动到另一个目录

linux - 如何从给定事件中删除匹配模式

logging - gcloud logging 读取命令不返回任何内容