[英]Linux tail on rotating log file using busybox
In my bash script am trying to monitor the out from the /var/log/message log file - and continue even when the file rotates (is re-created and started again). 在我的bash脚本中,我试图监视/ var / log / message日志文件中的内容-即使文件旋转(重新创建并再次启动)也要继续。 I tried using
tail -f filename
but quickly realised this is no good for when the file rotates. 我尝试使用
tail -f filename
但很快意识到这对于文件旋转时没有好处。
So there are lots of answers for using tail -F filename
or tail -f --retry filename
(and a few other variants). 因此,使用
tail -F filename
或tail -f --retry filename
(以及其他一些变体)有很多答案。
But on my embedded Linux I am using busybox which has a lightweight version of tail: 但是在我的嵌入式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).
So I can't do the usual tail -F ...
since that option is not implemented. 因此,我无法执行通常的
tail -F ...
因为该选项未实现。 The above document snippet is the latest busybox version - and mine is a bit older. 上面的文档片段是最新的busybox版本-而我的版本稍旧。
So I need another way of logging /var/log/messages since the file gets overwritten at a certain size. 因此,我需要另一种记录/ var / log / messages的方法,因为该文件会被覆盖为一定大小。
I was thinking of some simple bash line. 我在想一些简单的bash行。 So I saw things like inotifywait, but busybox does not have that.
因此,我看到了诸如inotifywait之类的东西,但是busybox没有。 I looked here:
我在这里看:
busybox docs and there is a inotifyd, but my version does not have that particular command. busybox文档,并且有inotifyd,但是我的版本没有该特定命令。 So I am wandering if there is a clever way of doing this with simple Linux commands/combination of commands like
watch
and tail -f
and cat/less/more
etc... I can't quite figure out what I need to do with the limited commands that I have :( 因此,我想知道是否有一种巧妙的方法可以通过简单的Linux命令/诸如
watch
and tail -f
和cat/less/more
等命令组合来完成此操作……我不太清楚我该怎么做我拥有的有限命令:(
How are the logs rotated? 日志如何旋转? Are you using a
logrotate
utility? 您是否正在使用
logrotate
实用程序? If yes, have you tried to add your line to postrotate
section in the config file? 如果是,是否尝试将行添加到配置文件中的
postrotate
部分?
from man logrotate 从男人logrotate
postrotate/endscript The lines between postrotate and endscript (both of which must appear on lines by themselves) are executed after the log file is rotated. postrotate / endscript在旋转日志文件后,将执行postrotate和endscript之间的行(两者都必须自己显示在行上)。 These directives may only appear inside of a log file definition.
这些伪指令只能出现在日志文件定义中。 See prerotate as well.
另请参见预旋转。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.