Rotate, Compress, and Manage Linux Log Files with logrotate
Learn how to configure Linux logrotate to rotate logs by time or size, retain limited copies, compress old files, run scripts, and manage application logs.
Log files record events from the operating system, services, and applications. Because many logs are written continuously, an unattended file can grow until the filesystem runs out of free space. This lesson explains how logrotate automates rotation, retention, compression, deletion, and service actions.
This lesson assumes familiarity with Linux paths, administrative access, basic shell and editor use, logs, services, and basic cron concepts. For background, see Linux file structure and managing Linux file ownership.
Why log rotation is necessary
Rotation is the process of retiring the current active log and starting or creating a replacement while preserving an older copy. For example, an active access_log might become access_log.1, while a new empty access_log receives new entries.
Without rotation, continuously written files can consume all available capacity on a filesystem. Rotation applies a policy that can:
- replace or rename the active log;
- retain a defined number of older copies or retain them for a defined period;
- compress older copies to reduce disk usage;
- delete copies that exceed the retention policy; and
- notify or reload a service after its log changes.
Retention is the number or duration of rotated copies kept before they are removed. Rotation is therefore both a naming operation and a disk-space management policy.
What logrotate does
logrotate is a Linux utility for applying automated log rotation and retention policies. It can rotate logs, create replacement files, compress old logs, remove expired copies, mail a log before it is removed, and execute shell commands before or after rotation.
logrotate is normally invoked regularly by a scheduler such as cron. The scheduler starts logrotate; logrotate then reads its configuration and determines whether each configured log should be rotated.
logrotate
Rotation is the maintenance step after an application or system component is generating log entries. A rotation policy cannot create useful entries by itself; the relevant service must first be configured to write to the target path.
Configuration locations and organization
/etc/logrotate.conf commonly establishes defaults for later log definitions. The include directive loads additional configuration files or a directory of files:
weekly
rotate 4
create
dateext
#compress
include /etc/logrotate.d
Here, weekly is a default interval, rotate 4 retains four older copies, create creates a replacement file, and dateext requests date-based suffixes. The commented compress line does not enable compression. The include directive loads policies from /etc/logrotate.d/.
A log-specific stanza can override applicable global defaults. This lets a high-volume application use a size threshold or a different schedule while ordinary logs use the global policy.
Logrotate stanza syntax
A stanza is a block containing one or more log paths followed by directives enclosed in braces:
/var/log/example.log {
weekly
rotate 4
compress
create
}
One definition controls the policy for the specified log file or files. Every directive inside the braces applies to that target and supersedes an applicable global default.
Multiple paths can be listed before the opening brace:
/var/log/service-a.log /var/log/service-b.log {
daily
rotate 7
}
Rotation schedules and size triggers
The interval directives express time-based rotation:
size sets a maximum-log-size trigger. Values can be expressed in bytes or with suffixes for kilobytes, megabytes, and gigabytes, such as 1k, 20M, or 2G. A small threshold is useful for demonstrating growth-based rotation:
/var/log/example_log {
size 1k
}
This policy is based on the file reaching 1 KB rather than waiting only for a daily, weekly, or monthly interval. In a real policy, choose a threshold appropriate to the log's write rate and available disk space.
Retention and deletion
The rotate directive specifies how many older rotated copies to retain:
/var/log/application.log {
weekly
rotate 4
}
With rotate 4, the policy keeps four older copies, subject to the naming and processing options in the stanza. When a copy reaches the end of its retention lifecycle, it is removed. If mail is configured, logrotate can mail that expiring log before removal.
Retention must balance troubleshooting needs against disk capacity. Keeping more copies provides a longer history but consumes more space, even when compression is enabled.
Naming rotated files
By default, rotated files use numbered suffixes. A sequence may look like:
access_log
access_log.1
access_log.2
access_log.3
On rotation, older numbers are shifted as needed and the active file is retired. The dateext directive uses a date suffix instead of a numeric suffix:
access_log
access_log-20140930
For a web server, a date-stamped access log makes the period covered by a file immediately visible. The exact date format depends on logrotate's date-extension behavior and the rotation date.
Compression and replacement files
compress compresses older rotated logs. gzip is the default compression method, so a retained file commonly ends with .gz:
/var/log/web/access_log {
weekly
rotate 4
compress
}
Compression reduces the disk space used by retained logs while preserving historical data. Compression can be enabled in global defaults or in one stanza, and it can be disabled for a policy when uncompressed files are required.
create creates a new empty active log after rotation. This is useful when the application expects the original path to exist. File mode, owner, and group can also be supplied with a more specific create form when required by the service:
/var/log/application.log {
weekly
create
}
Some applications keep an open file descriptor and need an explicit reload or reopen action after rotation. In that case, combine create with a postrotate hook.
Handling missing logs
missingok tells logrotate not to report an error when a configured log file is unavailable:
/var/log/optional-application.log {
daily
missingok
}
A file may be absent because the application has not started, has not generated an entry, uses a different path, or intentionally creates the file only under certain conditions. Verify the path first; use missingok when absence should not make an unattended rotation job fail.
Pre- and post-rotation scripts
prerotate and postrotate are hooks for shell commands run immediately before or after rotation. Each script block ends with endscript.
/var/log/application.log {
weekly
prerotate
# commands to run before rotation
endscript
postrotate
# commands to run after rotation
endscript
}
Use prerotate for preparation or notification before the file changes. Use postrotate to notify or reload a service so it closes the old file and begins writing to the new active file. Commands must be appropriate for the service and should be tested with administrative care.
Mailing expiring logs
The mail directive takes an email-address argument. It can send a rotated log when that log reaches the end of its retention lifecycle, before logrotate removes it:
/var/log/security-events.log {
weekly
rotate 4
mail admin@example.invalid
}
Mail delivery must be configured on the system for this feature to be useful. The directive does not by itself configure an email transport.
Core logrotate directives
Global defaults and per-log overrides
A global file might establish a default-style policy like this:
weekly
rotate 4
create
dateext
#compress
include /etc/logrotate.d
An individual stanza can override the weekly schedule. For example, this policy makes /var/log/wtmp monthly:
/var/log/wtmp {
monthly
}
The stanza's monthly directive supersedes the applicable global weekly default for that target. Other inherited defaults may still apply unless the stanza changes them.
Service-specific policies
Installed packages can provide files in /etc/logrotate.d/. Apache is a typical example: its access and error logs may need a service-specific schedule, retention count, compression policy, and post-rotation reload action rather than the same settings used for every system log.
# Conceptual Apache policy location
/etc/logrotate.d/apache2
Do not assume every distribution uses the same service name or exact file contents. Inspect the installed policy and adapt only the relevant settings. The important organization principle is that the package owns a focused policy while /etc/logrotate.conf supplies shared defaults and includes the directory.
Creating a custom application policy
Suppose an application writes entries to /var/log/example_log. Create a separate policy file under /etc/logrotate.d/, such as /etc/logrotate.d/example-app, with administrative privileges:
/var/log/example_log {
size 1k
compress
dateext
}
Each directive has a specific purpose:
/var/log/example_logidentifies the log managed by this stanza.size 1krequests rotation when the file reaches 1 KB, so growth rather than only a calendar interval triggers the policy.compresscompresses older rotated copies with gzip by default.dateextgives rotated copies date-based suffixes instead of the usual numeric suffixes.
A more complete production policy might add retention, a replacement file, and a service action:
/var/log/example_log {
size 1k
rotate 4
compress
dateext
missingok
create
postrotate
# reload or signal the application here
endscript
}
Before relying on the policy, confirm that the application actually writes to /var/log/example_log, that the path is correct, and that the service can write to the replacement file. A rotation configuration is maintenance for generated logs, not a substitute for configuring log generation.
Rotation lifecycle
- An application or system component writes entries to an active log.
- A scheduled invocation or a size condition causes logrotate to evaluate the policy.
- Any configured prerotate commands run.
- The active log is renamed or retired as a rotated copy.
- The rotated copy may receive a numeric or date-based name and may be compressed.
- A new active log may be created at the original path.
- Any configured postrotate commands run so a service can reopen or reload its log.
- Copies beyond the retention count are removed, or mailed before removal if configured.
Troubleshooting logrotate policies
The configured file does not exist
Check whether the application has created the file and verify the configured path. If the file is intentionally absent at times, add missingok to suppress the unavailable-file error.
Disk consumption remains high
Rotation may be too infrequent, the size threshold may be too large, too many copies may be retained, or compression may be disabled. Review the interval and size settings, reduce rotate where appropriate, and enable compress for retained logs.
The application uses the global schedule unexpectedly
The stanza may not contain its own interval or size rule, or its policy file may not be loaded. Add the intended daily, weekly, monthly, or size directive and confirm that include /etc/logrotate.d loads the directory.
Filenames are numbered instead of date-stamped
dateext may be absent, disabled, or ineffective for the applicable stanza. Enable it globally or inside the individual log definition.
The service stops writing after rotation
The service may still hold the old file open or may need to reopen its log. Add a suitable postrotate script containing the service-specific reload, reopen, or signal action.
Old logs are not compressed
Check whether compress is enabled in the global defaults or applicable stanza. An explicit disable setting or a policy override can prevent compression.
Exam-relevant notes
/etc/logrotate.confcontains global defaults and commonly includes/etc/logrotate.d/./etc/logrotate.d/is commonly used for package-specific and application-specific policies.- A stanza is a log path or group of paths followed by directives inside braces.
rotate 4means retain four older rotated copies.daily,weekly, andmonthlyare time-based intervals;size 1kis a size-based trigger.- Numeric names such as
access_log.1are the default style;dateextrequests names such asaccess_log-20140930. compressuses gzip by default for older logs, andcreatecreates a replacement active file.missingoksuppresses an error when the target log is absent.prerotateandpostrotatecontain commands and must end withendscript.mailcan send an expiring log before it is removed, provided mail delivery is configured.