Sub topic 4
Why Logs Must Be Rotated
This chapter assumes you can work comfortably on the Linux command line with sudo, know the basics of systemctl, and have looked inside /var/log before. It explains why log files need rotation, what rotation does, and what goes wrong when it is missing or misconfigured. The next chapter covers the logrotate configuration syntax in detail.
The problem: logs only grow
A daemon that writes to a log file appends to it for as long as it runs. Nothing in the kernel or the filesystem trims the file. A busy web server can write several gigabytes a day. A service stuck in an error loop can write far more.
Unbounded growth causes four kinds of trouble:
- Full disks. When
/var(or/, if/varis not a separate filesystem) fills up, writes fail. Databases stop, package installs fail, and services that write PID files or sockets can crash. The log that filled the disk is often the first casualty, so the evidence of the cause may be lost. - Unusable files. A 40 GB file is slow to open, search and copy.
grep,lessand editors all struggle. - No retention policy. Without a defined limit, you keep either everything or whatever happens to be left when the disk fills. Neither is a deliberate choice. Legal and security requirements usually say "keep 90 days" or "delete after 30 days", and a single endless file cannot meet that.
- Costly backups and transfers. One huge active file changes every day, so incremental backups copy it again each time.
Log rotation addresses all of this by splitting a log into time-bounded pieces and deleting the oldest pieces on a schedule.
What rotation does
Rotation renames the current log file, starts a fresh one, and removes generations that have aged out. With a rotation count of 4, the files shift along like this:
| Before rotation | After rotation |
|---|---|
| `app.log` (active) | `app.log` (new, empty) |
| `app.log.1` | `app.log.1` (was `app.log`) |
| `app.log.2` | `app.log.2` (was `app.log.1`) |
| `app.log.3` | `app.log.3` (was `app.log.2`) |
| `app.log.4` | deleted, replaced by what was `app.log.3` |
Most setups add compression, so older generations become app.log.2.gz, app.log.3.gz and so on. Text logs usually shrink by 80 to 95 percent, which multiplies how much history fits on a disk.
Rotation can be triggered by time (daily, weekly, monthly), by size, or by a combination. Time-based rotation gives predictable file boundaries, which helps when you search for "what happened on Tuesday". Size-based rotation gives a hard cap on the active file.
The open file handle problem
Step E in the diagram is the part that most often goes wrong. A running process does not hold a file name. It holds a file descriptor that points at an inode, which is the filesystem's record of the file. When you rename app.log to app.log.1, the inode does not change, so the process keeps writing into what is now app.log.1. The new empty app.log stays empty, and the service appears to have stopped logging.
There are two standard remedies.
- Signal the service to reopen its files. Many daemons reopen their logs when they receive SIGHUP, or on a dedicated reload command. logrotate runs this in a
postrotatescript. The example below sends SIGHUP through systemd:
sudo systemctl kill -s HUP myapp.service- Copy and truncate. logrotate's
copytruncateoption copies the file to the new name and then empties the original in place, so the process keeps the same inode. This works for programs that cannot reopen logs. It has a small window between the copy and the truncate in which lines can be lost. The process must also write in append mode, or it will keep its old offset and leave a file full of null bytes.
Prefer the signal approach when the daemon supports it. Use copytruncate only as a fallback.
You can see the problem directly. This command lists deleted files that a process still holds open, which is what happens when a log is removed without telling the service:
sudo lsof +L1 | grep -i logA row here means disk space is still in use even though ls and du no longer show the file. This is a classic cause of "the disk is full but I cannot find what is using it". Restarting or signalling the process releases the space.
Not every log needs logrotate
The sources of log data differ in who is responsible for trimming them:
| Source | Where it lands | Who limits its size |
|---|---|---|
| Applications writing their own files | `/var/log/<app>/` | logrotate, or the application itself |
| rsyslog output files | `/var/log/syslog`, `/var/log/messages`, `/var/log/secure` | logrotate |
| systemd journal | `/var/log/journal/` or `/run/log/journal/` | journald, using its own size and age settings |
| Binary login records | `/var/log/wtmp`, `/var/log/btmp` | logrotate |
The journal manages itself. Its limits are set in journald.conf and are not a job for logrotate. A common mistake is to add a logrotate rule for a journal file, which is unnecessary and can damage the journal. Journal limits are covered in a later chapter.
Choosing what to keep
Three decisions define a rotation policy.
- Frequency. How often should a new file begin? Daily suits most services. Weekly or monthly suits low-volume logs. Add a size limit if a log can spike.
- Generations. How many old files should remain? Multiply by the frequency to get the retention period. Daily rotation with 14 generations keeps two weeks.
- Compression. Compress old files to save space. If a service keeps writing to the old file for a moment after rotation, delay compression by one cycle. logrotate calls this
delaycompress.
Work out the disk budget before you pick numbers. If a service writes 2 GB per day uncompressed and compresses about 10 to 1, then 14 days of history needs roughly 14 × 0.2 GB for the archives plus up to 2 GB for the active file and one uncompressed generation. That is about 7 GB. Check that number against the size of /var/log, then leave headroom for spikes.
# How much space does each log directory use?
sudo du -sh /var/log/* 2>/dev/null | sort -rh | head
# How much free space is left on the filesystem holding /var/log?
df -h /var/logWhy this matters more with central logging
When you forward logs to a central host, rotation becomes a capacity question for the whole fleet. Each client still needs local rotation, because it keeps its own copies and queues messages while the server is unreachable. The central server receives the combined volume of every client, so a mistake there fills a disk that many servers depend on. Later chapters cover rotating and sizing the central store.
Common errors
- No rotation rule for a new application. Packages usually ship their own file in
/etc/logrotate.d/. Software installed by hand or from a tarball often does not, so check. - Service keeps writing to the old file. The postrotate signal is missing or wrong. Check with
lsofand with file sizes after a forced rotation. - Rotation succeeds but the new file has the wrong owner. If the service runs as an unprivileged user, the new file must be created with matching ownership and mode, or the service cannot write to it.
- Logs stored on a separate small partition. Retention that works on a large disk may overflow a 2 GB
/var/logpartition. Size limits matter more here.
Exercises
Exercise
A service writes to /var/log/myapp/app.log. After a rotation script renames the file to app.log.1, the new app.log stays empty and app.log.1 keeps growing. Why does this happen, and what are two ways to fix it?
Show answer
The process holds an open file descriptor tied to the inode, not to the name, so it keeps writing to the renamed file. Fix it by signalling the service to reopen its log files in a postrotate script (for example with SIGHUP), or by using copytruncate, which copies the file and empties the original in place so the inode stays the same. The signal is preferred because copytruncate can lose lines written between the copy and the truncate.
Exercise
A service writes 3 GB of log text per day and compresses about 10 to 1. You rotate daily and keep 10 generations. Roughly how much disk should you plan for?
Show answer
Ten compressed generations take about 10 × 0.3 GB = 3 GB. Add the active file (up to 3 GB) and one uncompressed generation if delaycompress is used (another 3 GB). That gives about 9 GB, so plan for roughly 10 GB with some headroom.