logrotate - Unix, Linux Command



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

    Tag Description
    -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
    }
    Advertisements