Managing log files effectively is an essential task for Linux sysadmin.
In this article, let us discuss how to perform following log file operations using UNIX logrotateutility.
Following are the key files that you should be aware of for logrotate to work properly.
/usr/sbin/logrotate – The logrotate command itself.
/etc/cron.daily/logrotate – This shell script executes the logrotate command everyday.
$ cat /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
/etc/logrotate.conf – Log rotation configuration for all the log files are specified in this file.

$ cat /etc/logrotate.conf
weekly
rotate 4
create
include /etc/logrotate.d
/var/log/wtmp {
monthly
minsize 1M
create 0664 root utmp
rotate 1
}
/etc/logrotate.d – When individual packages are installed on the system, they drop the log rotation configuration information in this directory. For example, yum log rotate configuration information is shown below.
$ cat /etc/logrotate.d/yum
/var/log/yum.log {
missingok
notifempty
size 30k
yearly
create 0600 root root
}
If you want to rotate a log file (for example, /tmp/output.log) for every 1KB, create the logrotate.conf as shown below.
$ cat logrotate.conf
/tmp/output.log {
size 1k
create 700 bala bala
rotate 4
}
This logrotate configuration has following three options:
Before the logrotation, following is the size of the output.log:
$ ls -l /tmp/output.log -rw-r--r-- 1 bala bala 25868 2010-06-09 21:19 /tmp/output.log
Now, run the logrotate command as shown below. Option -s specifies the filename to write the logrotate status.
$ logrotate -s /var/log/logstatus logrotate.conf
Note : whenever you need of log rotation for some files, prepare the logrotate configuration and run the logroate command manually.
After the logrotation, following is the size of the output.log:
$ ls -l /tmp/output* -rw-r--r-- 1 bala bala 25868 2010-06-09 21:20 output.log.1 -rwx------ 1 bala bala 0 2010-06-09 21:20 output.log
Eventually this will keep following setup of rotated log files.
Please remember that after the log rotation, the log file corresponds to the service would still point to rotated file (output.log.1) and keeps on writing in it. You can use the above method, if you want to rotate the apache access_log or error_log every 5 MB.
Ideally, you should modify the /etc/logrotate.conf to specify the logrotate information for a specific log file.
Also, if you are having huge log files, you can use: 10 Awesome Examples for Viewing Huge Log Files in Unix
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
rotate 4
}
copytruncate instruct logrotate to creates the copy of the original file (i.e rotate the original log file) and truncates the original file to zero byte size. This helps the respective service that belongs to that log file can write to the proper file.
While manipulating log files, you might find the sed substitute, sed delete tips helpful.
If you use the compress option as shown below, the rotated files will be compressed with gzip utility.
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
create 700 bala bala
rotate 4
compress
}
Output of compressed log file:
$ ls /tmp/output* output.log.1.gz output.log
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
create 700 bala bala
dateext
rotate 4
compress
}
After the above configuration, you’ll notice the date in the rotated log file as shown below.
$ ls -lrt /tmp/output* -rw-r--r-- 1 bala bala 8980 2010-06-09 22:10 output.log-20100609.gz -rwxrwxrwx 1 bala bala 0 2010-06-09 22:11 output.log
This would work only once in a day. Because when it tries to rotate next time on the same day, earlier rotated file will be having the same filename. So, the logrotate wont be successful after the first run on the same day.
Typically you might use tail -f to view the output of the log file in realtime. You can even combine multiple tail -f output and display it on single terminal.
For doing the rotation monthly once,
$ cat logrotate.conf
/tmp/output.log {
monthly
copytruncate
rotate 4
compress
}
Add the weekly keyword as shown below for weekly log rotation.
$ cat logrotate.conf
/tmp/output.log {
weekly
copytruncate
rotate 4
compress
}
Add the daily keyword as shown below for every day log rotation. You can also rotate logs hourly.
$ cat logrotate.conf
/tmp/output.log {
daily
copytruncate
rotate 4
compress
}
Logrotate allows you to run your own custom shell scripts after it completes the log file rotation. The following configuration indicates that it will execute myscript.sh after the logrotation.
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
rotate 4
compress
postrotate
/home/bala/myscript.sh
endscript
}
Logrotate automatically removes the rotated files after a specific number of days. The following example indicates that the rotated log files would be removed after 100 days.
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
rotate 4
compress
maxage 100
}
You can ignore the error message when the actual file is not available by using this option as shown below.
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
rotate 4
compress
missingok
}
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
create
compress
compresscmd /bin/bzip2
compressext .bz2
rotate 4
}
Article Number: 449
Posted: Fri, Nov 30, 2018 10:04 PM
Last Updated: Fri, Nov 30, 2018 10:04 PM
Online URL: http://kb.ictbanking.net/article.php?id=449