PCx3 avatar

logrotate usage with examples

pcx3

Published: 17 Apr 2022 › Updated: 17 Apr 2022logrotate usage with examples

logrotate usage with examples


For logrotate we create a configuration file inside /etc/logrotate.d/ directory and check if there is a logrotate file in /etc/cron.hourly/ - if not copy it from cron.daily

EXAMPLE 1: logrotate file that will only rotate a single file when its size reaches 2000MB, keep 7 files compressed and delete them after a week.

/var/log/apache2/domlogs/domain.com-ssl* {
    daily
    missingok
    notifempty
    rotate 7
    size 2000M
    maxsize 2000M
    maxage 7
    dateext
    compress
    create
}

EXAMPLE 2: Rotate all domlogs daily when they reach 200MB in size, keep 7 files rotated and delete them after a week. In this example, it is added that after rotation, httpd reload is performed, because otherwise Apache still writes to the same files after rotation.

/usr/local/apache/domlogs/* {
    daily
    missingok
    notifempty
    rotate 7
    size 200M
    maxsize 200M
    maxage 7
    dateext
    compress
    create
    sharedscripts
    postrotate
        /usr/bin/systemctl reload httpd
    endscript
}

after creating a file with the configuration (see examples above), we test logrotate with the command:

logrotate -d -v /etc/logrotate.d/apachefix
  • apachefix is file name
  • -d flag is used for dryrun - files are NOT rotated but only shown what would be rotated
  • -v flag stand for verbose - writes one line at a time what is/would be rotated
alt

You can see more about the logrotate itself as well as additional information at the following link: https://linux.die.net/man/8/logrotate

Leave logrotate usage with examples to:

Written by

Read more #wordpress posts


Best Posts From PCx3

We have not curated any of pcx3's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From PCx3