Learn How to Manage Various Logs using Logrotate in Linux

Logrotate is designed for administration of Ubuntu systems that generate giant numbers of log records. It allows automatic rotation, compression, removal, and mailing of log records. Each log file could also be handled every day, weekly, monthly, or when it grows. This article explains about -“Learn how to manage various log using logrotate in Linux”

To install logrotate, use the following command –

$ sudo apt-get install logrotate

The sample output should be like this –

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libecap3 squid-common squid-langpack
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 250 not upgraded.
Need to get 37.6 kB of archives.
After this operation, 116 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 logrotate amd64 3.8.7-2ubuntu2 [37.6 kB]
Fetched 37.6 kB in 0s (86.9 kB/s)
Selecting previously unselected package logrotate.
(Reading database ... 239112 files and directories currently installed.)
Preparing to unpack .../logrotate_3.8.7-2ubuntu2_amd64.deb ...
Unpacking logrotate (3.8.7-2ubuntu2) ...
Processing triggers for man-db (2.7.5-1) ...

To get more options about logrotate, use the following command –

$ logrotate --help

The sample output should be like this –

Usage: logrotate [OPTION...]
-d, --debug             Don't do anything, just test (implies -v)
-f, --force             Force file rotation
-m, --mail=command      Command to send mail (instead of `/usr/bin/mail')
-s, --state=statefile   Path of state file
-v, --verbose           Display messages during rotation
--version               Display version information

Help options:
-?, --help             Show this help message
--usage                Display brief usage message

The usage of logrotate, should be like this –

Usage: logrotate [-dfv?] [-d|--debug] [-f|--force] [-m|--mail=command]
[-s|--state=statefile] [-v|--verbose] [--version] [-?|--help]
[--usage] [OPTION...] <configfile>


To get the information about logrotate files, use the following command –

$ cd /etc/logrotate.d/
/etc/logrotate.d$ ls

The sample output should be like this –

apache2          dbconfig-common          munin-node          speech-dispatcher          upstart
apport           dpkg                     mysql-server        squid                      yum  
apt              jenkins                  pm-utils            squidguard
cacti            lightdm                  ppp                 ufw
cups-daemon munin rsyslog unattended-upgrades

To get the configuration of jenkins, use the following command –

$ sudo nano /etc/logrotate.d/jenkins

The sample output should be like this –

/var/log/jenkins/jenkins.log {
   rotate 52
   size 10k
   maxage 10
   compresscmd /bin/bzip2

How To Install Jenkins on Ubuntu

The summary of above result should be like as follows-

weekly – Log records are circled if the current weekday is less then the weekday of the last rotation or if extra then a week has passed seeing that the last rotation.

copytruncate  Truncate the long-established log file in position after growing a duplicate, instead of relocating the old log file and optionally growing a brand new one.

rotate 52  Log records are turned around 52 instances earlier than being eliminated or mailed to the deal with precise in a mail directive.

compress – The old versions of log records are compressed with gzip with the aid of default.

delaycompress  It delay compression of the previous log file to the subsequent rotation cycle.

notifempty  It do not rotate the log if it is empty.

 size 10k  logrotate runsif the filesize is equivalent to (or more prominent than) 10K.

 dateext  It archive old versions of log files adding a date extension like YYYYMMDD instead of readily adding number.

 maxage 10  Remove rotated logs older than 10 days.

 compresscmd  It specifies which command to use to compress log files.

Corn Job

To get the daily corn job of logrotate, use the following command as shown below –

$ cat /etc/cron.daily/logrotate

The sample ouput should be like this –


# Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
[ -e "$logfile" ] && echo "\"$logfile\" $date"
done >> status.clean
mv status.clean status

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

To get the status of logrotate, use the following command –

$ cat /var/lib/logrotate/status

The sample ouput should be like this –

logrotate state -- version 2
"/var/log/syslog" 2017-1-23-9:14:34
"/var/log/cacti/cacti.log" 2017-1-23-9:14:34
"/var/log/mail.log" 2017-1-23-9:14:34
"/var/log/kern.log" 2017-1-23-9:14:34
"/var/log/cups/error_log" 2016-12-17-14:5:58
"/var/log/mysql.log" 2017-1-23-9:0:0
"/var/log/ufw.log" 2017-1-23-9:0:0
"/var/log/cacti/rrd.log" 2017-1-9-11:0:0
"/var/log/lightdm/seat0-greeter.log" 2017-1-23-9:14:34
"/var/log/cacti/poller-error.log" 2017-1-9-11:0:0
"/var/log/munin/munin-update.log" 2017-1-17-9:58:3
"/var/log/speech-dispatcher/speech-dispatcher.log" 2017-1-23-9:0:0
"/var/log/debug" 2017-1-23-9:0:0
"/var/log/yum.log" 2017-1-17-9:0:0
"/var/log/munin/munin-node.log" 2017-1-23-9:14:34

In the above article, we have learnt about – Learn how to manage various log using logrotate in Linux. In our next articles, we will come up with more Linux based tricks and tips. Keep reading.

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 21-Jan-2020


Kickstart Your Career

Get certified by completing the course

Get Started