Home » Steps to Manage logs in Linux using Logrotate

In this tutorial, you will learn the simple steps to manage logs in Linux using logrotate. The Logrotate is used in all Linux distribution like Ubuntu, Amazon Linux, RHEL, CentOS for managing logs that are generated by the Linux machine.

Logrotate is best to manage logs by rotating logs, compress, and remove automatically. Logrotate also allows you to perform/handle logs every day, weekly, monthly or as per the size of the logs (as it grows).

Steps to install logrotate in Ubuntu:

$ sudo apt-get install logrotate #Output 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: logrotate 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) ...

Checkout logrotate options:

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

Example for Logrotate:

Logrotate path:

$ cd /etc/logrotate.d/ $ ls dpkg mysqld syslog yum zabbix-agent haproxy nginx apache2 dbconfig-common munin-node speech-dispatcher upstart jenkins pm-utils squidguard cacti lightdm ppp ufw

Example to configure logrotate for Jenkins logs

Create Jenkins file in “/etc/logrotate.d/” and below lines.

$ sudo nano /etc/logrotate.d/jenkins #Output /var/log/jenkins/jenkins.log { weekly copytruncate missingok rotate 52 compress delaycompress notifempty size 10k dateext maxage 10 compresscmd /bin/bzip2 }

The summary of this file:

weekly – Log records rotate weekly.

copytruncate –Truncate the log.

rotate 52 –Log records are turned around 52 instances earlier than being eliminated.

compress – The old versions of log records are compressed with gzip

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

notifempty – It does not rotate the log if it is empty.

size 10k – logrotate runs if the file size is equivalent to or more 10K.

dateext –It archive old versions of log files adding a date extension like YYYYMMDD

maxage 10 –Remove rotated logs older than 10 days.

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

You can check the status of logrotate:

$ cat /var/lib/logrotate/status #Output logrotate state -- version 2 "/var/log/syslog" 2018-4-1-9:14:34 "/var/log/cacti/cacti.log" 2018-4-1-9:14:34 "/var/log/mail.log" 2018-4-1-9:14:34 "/var/log/kern.log" 2018-4-1-9:14:34 "/var/log/cups/error_log" 2018-12-17-14:5:58 "/var/log/mysql.log" 2018-4-1-9:0:0 "/var/log/ufw.log" 2018-4-1-9:0:0 "/var/log/cacti/rrd.log" 2018-4-9-11:0:0 "/var/log/lightdm/seat0-greeter.log" 2018-4-1-9:14:34 "/var/log/cacti/poller-error.log" 2018-4-9-11:0:0 "/var/log/munin/munin-update.log" 2018-1-17-9:58:3 "/var/log/speech-dispatcher/speech-dispatcher.log" 2018-4-1-9:0:0 "/var/log/debug" 2018-4-1-9:0:0 "/var/log/yum.log" 2018-4-17-9:0:0 "/var/log/munin/munin-node.log" 2018-4-1-9:14:34

These are the steps to manage logs in Linux using logrotate.

Thanks for reading this article, you’ll also like to read below articles.