logrotate - rotates, compresses, and mails system logs.

NAME

logrotate - rotates, compresses, and mails system logs.

SYNOPSIS

  • logrotate [-dv] [--force] [-s|--state statefile] config_file ..

DESCRIPTION

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. 
Normally, logrotate is run as a daily cron job. It will not modify a log multiple times in one day unless the criterium for that log is based on the log’s size and logrotate is being run multiple times each day, or unless the -f or -force option is used. Any number of config files may be given on the command line. Later config files may override the options given in earlier files, so the order in which the logrotate config files are listed in is important. Normally, a single config file which includes any other config files which are needed should be used.

Options

TagDescription
-v Turn on verbose mode.
-d Turns on debug mode and implies -v. In debug mode, no changes will be made to the logs or to the logrotate state file.
-f, --force Tells logrotate to force the rotation, even if it doesn’t think this is necessary. Sometimes this is useful after adding new entries to logrotate, or if old log files have been removed by hand, as the new files will be created, and logging will continue correctly.
-m, --mail [command] Tells logrotate which command to use when mailing logs. This command should accept two arguments: 1) the subject of the message, and 2) the recipient. The command must then read a message on standard input and mail it to the recipient. The default mail command is /bin/mail -s.
-s, --state [statefile] Tells logrotate to use an alternate state file. This is useful if logrotate is being run as a different user for various sets of log files. The default state file is /var/lib/logrotate/sta- tus.
-usage Prints a short usage message.

EXAMPLES

Example-1:

Following are the key files that you should be aware of for logrotate to work properly:

$ 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

Example-2:

Logrotate size option: Rotate the log file when file size reaches a specific limit:

$ cat logrotate.conf

/tmp/output.log {
        size 1k
        create 700 bala bala
        rotate 4
}

Example-3:

Logrotate copytruncate option: Continue to write the log information in the newly created file after rotating the old log file:

$ cat logrotate.conf

/tmp/output.log {
         size 1k
         copytruncate
         rotate 4
}

Example-4:

Logrotate compress option: Compress the rotated log files :

$ 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

Example-5:

Logrotate dateext option: Rotate the old log file with date in the log filename:

$ cat logrotate.conf

/tmp/output.log {
        size 1k
        copytruncate
        create 700 test test
        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 test test 8980 2010-06-09 22:10 output.log-20100609.gz
-rwxrwxrwx 1 test test    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.

Example-6:

Logrotate monthly, daily, weekly option: Rotate the log file weekly/daily/monthly.

$ 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
}

Example-7:

Logrotate postrotate endscript option: Run custom shell scripts immediately after log rotation:

$ cat logrotate.conf

/tmp/output.log {
        size 1k
        copytruncate
        rotate 4
        compress
        postrotate
               /home/test/myscript.sh
        endscript
}

Example-8:

Logrotate maxage option: Remove older rotated log files:

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
}

Example-9:

Logrotate missing ok option: Dont return error if the log file is missing:

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
}

Example-10:

Logrotate compresscmd and compressext option: Specify compression command for the log file rotation:

$ cat logrotate.conf

/tmp/output.log {
        size 1k
        copytruncate
        create
        compress
        compresscmd /bin/bzip2
        compressext .bz2
        rotate 4
}
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
LVM: Recovering Physical Volume Metadata
Viewed 12841 times since Sat, Jun 2, 2018
Linux 20 Netstat Commands for Linux Network Management
Viewed 9180 times since Mon, Sep 21, 2020
How to automate SSH login with password? ssh autologin
Viewed 2390 times since Fri, Jun 8, 2018
RHCS6: Debug and test multicast traffic between two hosts
Viewed 6242 times since Sun, Jun 3, 2018
RHEL: Bonding network interfaces
Viewed 3362 times since Sat, Jun 2, 2018
Oracle Linux 7 – How to audit changes to a trusted file such as /etc/passwd or /etc/shadow
Viewed 2654 times since Wed, Jul 25, 2018
Linux RedHat How To Create An RPM Package
Viewed 2770 times since Sun, Jan 9, 2022
Easily Monitor CPU Utilization in Linux Terminal With Stress Terminal UI
Viewed 3658 times since Thu, Apr 18, 2019
Setting up encrypted tunnel using stunnel
Viewed 2138 times since Fri, Sep 28, 2018
Configuring VLAN interfaces in Linux
Viewed 4939 times since Mon, May 21, 2018