简体   繁体   中英

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). I tried using tail -f filename but quickly realised this is no good for when the file rotates.

So there are lots of answers for using tail -F filename or tail -f --retry filename (and a few other variants).

But on my embedded Linux I am using busybox which has a lightweight version of 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. The above document snippet is the latest busybox version - and mine is a bit older.

So I need another way of logging /var/log/messages since the file gets overwritten at a certain size.

I was thinking of some simple bash line. So I saw things like inotifywait, but busybox does not have that. I looked here:

busybox docs and there is a inotifyd, but my version does not have that particular command. 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 :(

How are the logs rotated? Are you using a logrotate utility? If yes, have you tried to add your line to postrotate section in the config file?

from man 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. These directives may only appear inside of a log file definition. See prerotate as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM