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
Zabijanie wszystkich procesów użytkownika
Viewed 3186 times since Thu, May 24, 2018
Linux RAID Mdadm Cheat Sheet
Viewed 6071 times since Fri, May 15, 2020
debian How to check Debian CVE status using python script
Viewed 4100 times since Sun, Sep 23, 2018
PROCESSOR AND MEMORY INFORMATION
Viewed 6163 times since Sat, Jun 2, 2018
Monitoring bezpieczeństwa Linux: integracja auditd + OSSEC cz. I
Viewed 2957 times since Fri, Apr 5, 2019
RHCS6: Install a two-node basic cluster
Viewed 3946 times since Sun, Jun 3, 2018
Using Official Redhat DVD as repository
Viewed 11800 times since Mon, Oct 29, 2018
How to stop and disable auditd on RHEL 7
Viewed 41573 times since Tue, Aug 6, 2019
How To Use the Linux Auditing System on CentOS 7
Viewed 4551 times since Fri, Apr 5, 2019
RHCS6: Extend an existing Logical Volume / GFS2 filesystem
Viewed 3836 times since Sun, Jun 3, 2018