Log File Rotation
From MythTV
Running MythTV's frontend and backend with logging can help diagnose any problem that may arise. The problem is these log files can grow quite large and cause other problems (like using all your harddrive space). Here are some user posted logrotate scripts designed to handle rotation and deletion of old logs.
Contents |
/etc/logrotate.d
Most users will find the /etc/logrotate.d folder contains some log rotation scripts already. Choose one of the scripts below that best suits how you would like to handle your logs, create the new file with the same permissions as the other scripts, and paste your chosen script. You can name it anything you want; in the example below it is called mythtv. It is assumed that your log files are in /var/log/mythtv/ and names mythfrontend.log and mythbackend.log.
To manually run logrotate to test your script you can use
logrotate -f -s /var/lib/logrotate.status /etc/logrotate.d/mythtv
Make sure to substitute the location for your status and logrotate.d/mythtv files!
Starting with the 0.19 release you can send a signal to the backend to reload the log files. This is useful after the rotation to write to the new log file, rather than the old.
kill -HUP `cat /var/run/mythtv/mythbackend.pid`
Scripts
User Script 1
This is a basic script with most of the useful commands. This will wait until the log reaches 10M before rotating, check daily, compress the log file with tar.gz, allow for 7 logs before removing the oldest ones, and will copy the log file so that MythTV can continue to write to a new clean log file.
/var/log/mythtv/mythfrontend.log {
copytruncate
daily
size 10M
missingok
rotate 7
compress
notifempty
}
User Script 2
This script shows how to compress your logs with bzip2 instead of gzip.
/var/log/mythtv/mythbackend.log {
notifempty
missingok
compress
compresscmd /usr/bin/bzip2
uncompresscmd /usr/bin/bunzip2
compressext .bz2
weekly
rotate 4
size 10M
}
User Script 3
This is another one that keeps daily logs, bzips them, and gets the backend to write to the new log.
/var/log/mythtv/mythbackend.log {
notifempty
missingok
copytruncate
compress
compresscmd /bin/bzip2
uncompresscmd /bin/bunzip2
compressext .bz2
dateext
daily
rotate 20
postrotate
kill -HUP `cat /var/run/mythtv/mythbackend.pid`
endscript
}
Basic logrotate Commands
Here are some of the basic logrotate commands and what they do.
compress
Old versions of log files are compressed with gzip(1) by default. See also
nocompress.
compresscmd
Specifies which command to use to compress log files. The default is gzip(1).
See also compress.
uncompresscmd
Specifies which command to use to uncompress log files. The default is gun-
zip(1).
compressext
Specifies which extension to use on compressed logfiles, if compression is
enabled. The default follows that of the default compression command (.gz).
compressoptions
Command line options may be passed to the compression program, if one is in use.
The default, for gzip, is "-9" (maximum compression).
copy Make a copy of the log file, but don't change the original at all. This option
can be used, for instance, to make a snapshot of the current log file, or when
some other utility needs to truncate or pare the file. When this option is used,
the create option will have no effect, as the old log file stays in place.
copytruncate
Truncate the original log file to zero size in place after creating a copy,
instead of moving the old log file and optionally creating a new one. It can be
used when some program cannot be told to close its logfile and thus might con-
tinue writing (appending) to the previous log file forever. Note that there is a
very small time slice between copying the file and truncating it, so some logging
data might be lost. When this option is used, the create option will have no
effect, as the old log file stays in place.
create mode owner group
Immediately after rotation (before the postrotate script is run) the log file is
created (with the same name as the log file just rotated). mode specifies the
mode for the log file in octal (the same as chmod)(2), owner specifies the user
name who will own the log file, and group specifies the group the log file will
belong to. Any of the log file attributes may be omitted, in which case those
attributes for the new file will use the same values as the original log file for
the omitted attributes. This option can be disabled using the nocreate option.
daily Log files are rotated every day.
dateext
Archive old versions of log files adding a daily extension like YYYYMMDD instead
of simply adding a number.
delaycompress
Postpone compression of the previous log file to the next rotation cycle. This
only has effect when used in combination with compress. It can be used when some
program cannot be told to close its logfile and thus might continue writing to
the previous log file for some time.
extension ext
Log files are given the final extension ext after rotation. If compression is
used, the compression extension (normally .gz) appears after ext.
ifempty
Rotate the log file even if it is empty, overriding the notifempty option
(ifempty is the default).
missingok
If the log file is missing, go on to the next one without issuing an error mes-
sage. See also nomissingok.
monthly
Log files are rotated the first time logrotate is run in a month (this is nor-
mally on the first day of the month).
nocompress
Old versions of log files are not compressed. See also compress.
nocopy Do not copy the original log file and leave it in place. (this overrides the
copy option).
nocopytruncate
Do not truncate the original log file in place after creating a copy (this over-
rides the copytruncate option).
nocreate
New log files are not created (this overrides the create option).
nodelaycompress
Do not postpone compression of the previous log file to the next rotation cycle
(this overrides the delaycompress option).
nomissingok
If a log file does not exist, issue an error. This is the default.
notifempty
Do not rotate the log if it is empty (this overrides the ifempty option).
rotate count
Log files are rotated count times before being removed or mailed to the address
specified in a mail directive. If count is 0, old versions are removed rather
then rotated.
size size[G|M|k]
Log files are rotated when they grow bigger then size bytes. If size is followed
by M, the size if assumed to be in megabytes. If the G suffix is used, the size
is in gigabytes. If the k suffix is used, the size is in kilobytes. So size 100,
size 100k, size 100M and size 1G are all valid.
weekly Log files are rotated if the current weekday is less then the weekday of the last
rotation or if more then a week has passed since the last rotation. This is nor-
mally the same as rotating logs on the first day of the week, but if logrotate is
not being run every night a log rotation will happen at the first valid opportu-
nity.
