Apr 28, 2011

Logrotate in Linux

Logrotate in Linux

Release:
RedHat Enterprise Linux 5
SUSE Linux Enterprise Server 10

Problem:
Need to rotate the application log in the frequent interval

Solution:

logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

  1. Install the required rpm

# yum install logrotate

  1. Create a Configuration file like the below

# vi /etc/logrotate.d/ibmhttp
/IBM/HTTPServer/logs/access_log {
weekly
rotate 5
compress
missingok
notifempty
sharedscripts
postrotate
/bin/kill -HUP `cat /IBM/HTTPServer/logs/httpd.pid` 2> /dev/null || true
endscript
}

Where,

/IBM/HTTPServer/logs/access_log - Log file location

weekly - This is used to rotate log files weekly (eg: daily, weekly, monthly)

rotate 5 - This specifies the number of times to rotate a file before it is deleted. A count of 0 (zero) means no copies are retained. A count of 5 means five copies are retained.

compress - This is used to compress the rotated log file with gzip

missingok - If the log file is missing, go on to the next one without issuing an error message

notifempty - This does not rotate the log file if it is empty

sharedscripts - Normally, prerotate and postrotate scripts are run for each log which is rotated, meaning that a single script may be run multiple times for log file entries which match multiple files. If sharedscript is specified, the scripts are only run once, no matter how many logs match the wildcarded pattern. However, if none of the logs in the pattern require rotating, the scripts will not be run at all

postrotate to endscript - These are statements that enclose commands to be executed after a log file has been rotated. The postrotate and endscript keywords must appear on a line by themselves.


  1. We can rotate the logs depends on the log size also, using the below option

size size - With this, the log file is rotated when the specified size is reached. Size may be specified in bytes (default), kilobytes (sizek), or megabytes (sizem).

Note: If size and time interval options are specified at same time, only size option take effect. it causes log files to be rotated without regard for the last rotation time. If both log size and timestamp of a log file need to be considered by logrotate, the minsize option should be used. logrotate will rotate log file when they grow bigger than minsize, but not before the additionally specified time interval.

  1. The other options used in the logrotate scripts are listed below

/bin/kill -HUP `cat /IBM/HTTPServer/logs/httpd.pid` 2> /dev/null || true - Reload/HUP: Sending the HUP or restart signal to the parent causes it to kill off its children like in TERM, but the parent doesn't exit. It re-reads its configuration files, and re-opens any log files. Then it spawns a new set of children and continues serving hits.

/bin/kill -USR1 `cat /IBM/HTTPServer/logs/httpd.pid` 2> /dev/null || true - Graceful/USR1: The USR1 or graceful signal causes the parent process to advise the children to exit after their current request (or to exit immediately if they're not serving anything). The parent re-reads its configuration files and re-opens its log files. As each child dies off the parent replaces it with a child from the new generation of the configuration, which begins serving new requests immediately.

maxage count - Remove rotated logs older than days. The age is only checked if the logfile is to be rotated.

nocompress - Old versions of log files are not compressed with gzip.

nocopytruncate - Do not truncate the original log file in place after creating a copy (this overrides the copytruncate option).

copytruncate - Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one.

mail address - When a log is rotated out-of-existence, it is mailed to






.