Last active
February 26, 2021 01:54
-
-
Save kfish/42526e58badd63a75154 to your computer and use it in GitHub Desktop.
How to monitor a logrotated file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(Read the comments) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The scenario: some daemon is writing a log file, and you wish to monitor that log file (ie. read it and do something with the logged information). Independently, the log file is being rotated: ie. the current file gets renamed to log.1, and the daemon will write to a new version of the file.
We can simulate this scenario as follows:
Daemon
We simulate a daemon that writes data to a local file
t.log
at regular intervals:Logrotate
We configure logrotate to manage this file with the following local config file
logrotate.conf
:To test logrotate on this local file without disturbing the system's logrotate, we use the
-s
option to specify a local status file. We use the-f
option to force a logrotate now (by default logrotate will refuse to rotate files newer than a day old):To rotate every 3 seconds, and list the collection of log files after each rotation:
Tail
We can monitor this rotated log file with
tail -F
, which will follow the file name. It periodically reports to stderr;strace
will allow us to see the system calls thattail -F
uses to track renames:The resulting
strace.out
reveals: