Log Rotation With logrotate: A Practical Guide
A log file that is never cleaned up will eventually fill the disk. A web server writing to a single access file for a year can produce a file so large that it is slow to open and dangerous to leave alone. Log rotation solves this by regularly renaming the current file, starting a fresh one and removing or compressing the old ones.
On Linux, the standard tool for this job is logrotate. It reads configuration files, decides which logs need attention based on size or age, and performs the rotation. Once you understand its handful of directives, you can manage logs for almost any application.
How Rotation Works
When logrotate processes a log file, it typically does this:
- Renames
app.logtoapp.log.1. - Shifts older copies up:
app.log.1becomesapp.log.2, and so on. - Optionally compresses older copies.
- Creates a new, empty
app.log. - Tells the application to reopen its log file, if needed.
- Deletes copies beyond the number you chose to keep.
The count you keep and the compression you use decide how much history remains and how much space it takes.
When It Runs
On many distributions, logrotate runs once a day, started by a scheduled timer or a cron job. Each run reads the main file /etc/logrotate.conf and any files in the directory /etc/logrotate.d/. Packages usually drop their own small configuration file into that directory, which is why many logs are rotated without you doing anything.
Because the tool is usually run daily, a setting such as hourly rotation only works if you also arrange for logrotate to be invoked more often than once a day.
A Sample Configuration
Here is a typical configuration for an application that writes to a log under /var/log/myapp/:
/var/log/myapp/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 myapp adm
sharedscripts
postrotate
systemctl reload myapp > /dev/null 2>&1 || true
endscript
}
Each line is a directive. Here is what they mean.
The Most Useful Directives
- daily, weekly, monthly: how often to rotate. Choose based on log volume.
- rotate N: keep N old copies, deleting anything older. With
dailyandrotate 14, you keep about two weeks. - size SIZE: rotate when the file grows beyond a size, such as
size 100M, instead of by schedule. A related option,maxsize, rotates early if the file exceeds that size even before the scheduled time. - compress: compress rotated logs with gzip to save space.
- delaycompress: postpone compression to the next cycle, so the most recent rotated file remains uncompressed. It is helpful for programs that may still write to the old file briefly.
- missingok: do not report an error if the log file does not exist.
- notifempty: skip rotation when the file is empty.
- create MODE OWNER GROUP: make a new log file with these permissions and ownership after rotation.
- dateext: use a date in the rotated file name rather than a number, which makes files easier to identify.
- sharedscripts: run the pre and post scripts once for all matching files, rather than once per file.
- postrotate / endscript: commands to run after rotation, typically used to make the application reopen its file.
- copytruncate: copy the log and then empty the original in place. It is useful for programs that cannot reopen files, but a few lines may be lost between the copy and the truncation.
Make the Application Release the Old File
This is the step beginners most often forget. After logrotate renames the file, the running program may still hold the old file open and keep writing to it, even though it now has a different name. The new empty file then stays empty while the old one keeps growing.
There are two ways to deal with it:
- Use a
postrotatescript that tells the program to reopen its logs, usually with a reload or a signal such asSIGHUP. Many services have a built-in reopen or reload command for this. - Use
copytruncatewhen the program has no such feature, accepting the small risk of losing a few lines.
Prefer the first approach whenever the application supports it.
Test Before You Trust
Never wait for the nightly run to discover a mistake. Check your configuration with debug mode, which shows what would happen without changing anything:
sudo logrotate -d /etc/logrotate.d/myapp
To force a real rotation immediately for testing, use:
sudo logrotate -f /etc/logrotate.d/myapp
Then check that the old file was renamed, a new one was created with the right permissions, and the application is writing to the new file.
Logrotate remembers when each file was last rotated in a state file, commonly under /var/lib/logrotate/. If a rotation seems skipped, look at that file, since logrotate uses it to decide whether enough time has passed.
Rotating Logs From Containers and the Journal
Applications that log to standard output inside containers are handled by the container runtime, which has its own rotation settings, so check there instead. Systems using the systemd journal manage size through the journal's own limits rather than logrotate. Use logrotate for plain text files on disk.
Common Pitfalls
- Wrong permissions after rotation. If the new file is created as root while the application runs as another user, it may fail to write. Set
createwith the right owner. - No reload of the application. As described, the program keeps writing to the old file.
- Wildcards that match too much. A pattern like
/var/log/*can catch files you did not intend to rotate. - Too few copies kept. If an incident is noticed after two weeks and you only keep a week, the evidence is gone. Align the count with your retention needs.
- Large files and compression time. Compressing very large logs consumes CPU and I/O. Rotate by size to keep individual files manageable.
Frequently Asked Questions
How do I rotate logs more than once a day?
Logrotate itself does not run continuously, so schedule it more often, for example with an hourly timer or cron entry, and use directives such as hourly or maxsize that act when it runs. Make sure the configuration is valid for each invocation.
What is the difference between copytruncate and a postrotate reload?
With a reload, the application closes the renamed file and opens a fresh one, so no data is lost. With copytruncate, logrotate copies the file contents and then empties the original, which works for programs that cannot reopen files but may drop messages written during the short gap.
Where do I put my own logrotate rules?
Create a separate file in /etc/logrotate.d/, named after your application. This keeps your configuration tidy, avoids editing the main file and survives package updates. Test it with the debug option before relying on it.