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
An easier way to manage disk decryption at boot with Red Hat Enterprise Linux 7.5 using NBDE
Viewed 1490 times since Mon, Aug 6, 2018
Get UUID of Hard Disks [Update]
Viewed 509 times since Tue, Jul 17, 2018
Setting up encrypted tunnel using stunnel
Viewed 408 times since Fri, Sep 28, 2018
Index » Community Contributions » System encryption using LUKS and GPG encrypted keys for arch linux
Viewed 538 times since Fri, Jul 13, 2018
stunnel basics and pki standards
Viewed 918 times since Fri, Sep 28, 2018
Securing /tmp and shm partitions
Viewed 107 times since Fri, May 15, 2020
SYS: Configure a local repository. local repo
Viewed 613 times since Mon, Oct 29, 2018
CentOS / RHEL : How to move a Volume Group from one system to another
Viewed 569 times since Mon, Jan 28, 2019
RHCS6: Debug and test multicast traffic between two hosts
Viewed 623 times since Sun, Jun 3, 2018
How to schedule crontab in Unix Operating Systems
Viewed 502 times since Fri, Jun 8, 2018