Wednesday 8 February 2012

How to rotate logs in linux : Logrotate

We need use tool called logrotate. 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. 

The Configuration file for the tool is  /etc/logrotate.conf

/etc/logrotate.d – When individual packages are installed on the system, they drop the log rotation configuration information in this directory.


# cat /etc/logrotate.d/yum
 /var/log/yum.log {
    missingok
    notifempty
    size 30k
    yearly
    create 0600 root root
}
We can just check the /etc/logrotate.conf file

# vim /etc/logrotate.conf

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# uncomment this if you want your log files compressed
compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d


# no packages own wtmp -- we'll rotate them here
/var/log/wtmp {
monthly
minsize 1M
create 0664 root utmp
rotate 1
}
Service or server specific configurations stored in /etc/logrotate.d directory, for example here is sample apache logrotate configuration file.

# cat /etc/logrotate.d/httpd
/var/log/httpd/*.log {
monthly 
minsize 1M

rotate 10  Log files are rotated 52 times before being removed or mailed to the address
copytruncate  -----: Continue to write the log information in the newly created file after rotating the old log file 
notifempty  -----: Do not rotate the log if it is empty 
dateext          ----: Rotate the old log file with date in the log filename
/home/script.sh
endscript  ----:  Run custom scripts immediately after log rotation
maxage 100  ----:  Remove older rotated log files, after 100 days.
missingok   ----:  Dont return error if the log file is missing
compress    ---:   Indicates that compression should be done.
compresscmd /bin/bzip2 ----: Specify what type of compression command should be used
compressext .bz2 ----: Specify the extension on the rotated log file. Without this option, the rotated file would have the default extension as .gz

 

No comments:

Post a Comment