logrotate: Stopping Any Log File From Quietly Filling the Disk

The tool that has been rotating your logs since before you knew it existed

Contents

The disk-full alert always arrives at the worst possible time, and nine times out of ten the culprit is the same: a text file, somewhere in /var/log, that some daemon has been appending to for eight months without anyone telling it to stop. I have chased this exact page across a mail relay, a reverse proxy, and once a home-grown Python script that logged every HTTP request to a file it opened once and never closed. In every case the fix already existed on the box. It is called logrotate, it has been shipping with Debian, Ubuntu, RHEL and every derivative since the mid-1990s, and most of us only meet it properly the day it fails to cover something.

logrotate is a small program that runs once a day, walks a list of rules, and decides for each one whether the matching file is due for rotation — it does not watch files continuously, and it holds no state about what your application is doing right now. On Debian and Ubuntu that trigger is a systemd timer (or /etc/cron.daily on older installs) calling /usr/sbin/logrotate /etc/logrotate.conf. Understanding that it is a periodic batch job explains most of the confusing behaviour people hit: a file that grew to 4GB in the last hour will still wait for the next scheduled run, and a rule you just added will not do anything until that run happens (or you trigger it by hand).

Why the stock config already covers most of your box

Open /etc/logrotate.d/ on any fresh Debian or Ubuntu install and you will find a rule file per package: rsyslog, apt, dpkg, sometimes nginx or apache2 if you installed them from the distro repos. Package maintainers write these because they know exactly which files their software writes and how often they grow. This is why most people never think about logrotate — the packages that need it already ship a working rule, and /etc/logrotate.conf includes the whole directory:

The interior of an opened 3.5-inch hard disk drive showing the platter and actuator arm.
Every log line eventually lands on a platter like this one, and a disk that fills up stops the services writing to it. Photo: ExcelStor 80 GB Hard Disk Interior by Fletcher6, CC BY 4.0, via Wikimedia Commons.
1
2
3
4
5
6
# /etc/logrotate.conf (excerpt, Debian/Ubuntu default)
weekly
rotate 4
create
compress
include /etc/logrotate.d

Those four directives are the defaults every per-package rule inherits unless it overrides them: rotate weekly, keep four generations, create a fresh empty file with the right permissions after rotating, and gzip the rotated copies. The gap is anything that did not ship a rule — software you built yourself, a container writing to a bind-mounted log file, or a script you wrote in an afternoon that has quietly been the biggest file on the box for a year.

Writing your own rule

A logrotate rule is a stanza naming one or more paths (globs work) followed by directives in braces. Here is a rule for an application that logs to a fixed path and does not know or care that it is being rotated out from under it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# /etc/logrotate.d/myapp
/var/log/myapp/*.log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 0640 myapp myapp
    sharedscripts
    postrotate
        systemctl kill -s HUP myapp.service >/dev/null 2>&1 || true
    endscript
}

Each directive earns its place:

  • daily / rotate 14 — rotate once a day, keep two weeks of history. Tune this to how much you’d actually want to grep through during an incident versus how much disk you’re willing to spend.
  • compress / delaycompress — gzip old logs, but leave the most recently rotated one uncompressed for one more cycle. This matters if anything might still be writing to the just-rotated file for a moment after the switch — you don’t want to gzip a file mid-write.
  • missingok — don’t error out if the glob matches nothing, which happens on a fresh install before the app has logged anything.
  • notifempty — skip rotation if the file is empty; there is no point creating an empty gzip.
  • create 0640 myapp myapp — after rotation, create a new empty file with these permissions and this owner, so the application can keep writing to the same path without a restart.
  • postrotate / endscript — the part that actually matters for correctness. Many daemons hold the log file open by file descriptor, not by path. Renaming myapp.log to myapp.log.1 does not make the running process notice; it just keeps writing into a file that now has no name (the inode still exists, the disk space is not actually freed, and nothing new shows up at the path you’re tailing). The postrotate script tells the application to reopen its log file — a SIGHUP, a systemctl reload, or an app-specific reload command, depending on what the software supports.

That last point is the one that catches people. I have debugged a “logrotate isn’t working” ticket that turned out to be logrotate working perfectly — the file was rotated, compressed, and a fresh empty one created — while the daemon kept appending to the deleted-but-still-open original, invisible from the filesystem, until the disk filled anyway. df and du disagreeing about how much space is used is the tell: du sums file sizes it can see by walking the tree, df reports actual block usage, and a large deleted-but-open file counts for df but not du. If you’ve hit “no space left on device” and the numbers don’t add up, du and ncdu are the tools for finding what filled the disk — and an open deleted log file held by a stale process is a classic cause.

Testing without waiting for the timer

You do not have to wait a day to find out if a rule works. logrotate -d does a dry run and prints what it would do without touching anything:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ sudo logrotate -d /etc/logrotate.d/myapp
reading config file /etc/logrotate.d/myapp
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /var/log/myapp/*.log  after 1 days (14 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/myapp/app.log
  log needs rotating

Once you’re happy with the dry run, force an actual rotation with -f:

1
$ sudo logrotate -f /etc/logrotate.d/myapp

If a file that should rotate isn’t, the usual reason is the state file. logrotate tracks the last rotation time per file in /var/lib/logrotate/status (Debian/Ubuntu) so it doesn’t rotate the same file twice on the same day if the daily job runs more than once. If you edited a rule and it seems to be ignoring the new schedule, check whether a stale entry in that status file is telling logrotate it already rotated today:

1
2
$ grep myapp /var/lib/logrotate/status
"/var/log/myapp/app.log" 2024-11-13-09:15:02

Delete the line (or the whole file, which is safe — it just forces every rule to be re-evaluated fresh) and re-run.

Rotating by size instead of by schedule

daily/weekly/monthly rotate on a calendar, which is fine for steady, predictable logging but wrong for anything bursty. A debug log that stays quiet for weeks and then floods with ten thousand lines during an incident will happily grow past any sane size between scheduled runs. size (or maxsize combined with a time directive) rotates as soon as a file crosses a threshold, checked every time the timer fires:

1
2
3
4
5
6
7
8
/var/log/myapp/debug.log {
    daily
    maxsize 100M
    rotate 5
    compress
    missingok
    create 0640 myapp myapp
}

maxsize here means “rotate daily, but also rotate early if it hits 100MB first” — the two conditions are additive, both able to trigger a rotation independently. Use plain size 100M on its own if you want size to be the only trigger, ignoring the calendar entirely. Either way, remember the batch-job caveat from earlier: logrotate still only checks this when the timer runs, typically once a day, so maxsize caps how big the file gets by the next check, a bound measured in hours rather than seconds. A hard real-time cap needs to live in the application’s own log-size limiting, since that’s the only layer actually watching the file continuously as it writes.

Two more directives worth knowing about once the basics are working. dateext renames rotated files with the date instead of a bare number (app.log-20241114 instead of app.log.1), which makes it obvious at a glance when a given rotation happened — genuinely useful once you’re keeping more than a handful of generations. olddir /var/log/myapp/archive moves rotated files into a separate directory instead of leaving them alongside the live log, which keeps ls on the active log directory readable and makes it trivial to point a separate backup job at just the archive.

Common failures and how to read them

Three problems account for nearly everything that goes wrong once you’ve written a rule.

“Permission denied” in the postrotate script. logrotate itself typically runs as root via the systemd timer, but the command inside postrotate runs with whatever privileges the invoking process has — which is root, but the target command (a systemctl kill -s HUP, a curl to a local admin socket) can still fail for its own reasons unrelated to logrotate. Check the actual error, not just the fact that logrotate reported the postrotate script exited non-zero; run the command by hand as root to see the real failure.

Duplicate rotation on the same day. Covered above — check /var/lib/logrotate/status for a stale timestamp before assuming your rule is broken.

Rotated files never actually shrinking disk usage. This is the deleted-but-open-file problem from the postrotate section, and it’s worth restating because it is the single most common “logrotate isn’t working” report I’ve seen: the rotated files on disk look correctly sized and compressed, but df still shows the volume nearly full. lsof +L1 lists open files with a link count of zero — files that have been unlinked (deleted, or in this case renamed away by logrotate) but are still held open by a process, which is exactly the case where their disk space isn’t actually freed until that process closes or reopens the descriptor:

1
2
$ sudo lsof +L1 | grep myapp
myapp    4821  myapp   7w   REG  253,0  4294967296    0  1441792 /var/log/myapp/app.log (deleted)

That output — a 4GB “deleted” file still open by PID 4821 — is the smoking gun. The fix is always the same: make the application reopen its log file, either via the postrotate script you should have written, or by restarting the process if the software has no reload signal.

Where logrotate stops being the right tool

logrotate is a great fit for plain text files written by long-running processes that respect a reload signal. It is the wrong tool in two situations I run into constantly on a homelab.

First, containerised logging. If your containers write to stdout/stderr and Docker captures it with the default json-file driver, that file lives under Docker’s own storage path, not /var/log, and logrotate rules pointed at /var/log will never see it. Docker has its own log rotation you configure per-daemon or per-container instead — I cover the actual mechanics, and the disk-filling failure mode when it’s left unset, in a separate piece on Docker log rotation.

Second, structured or high-volume logs you actually want to query later rather than just archive and eventually delete. Grepping through fourteen gzipped files for one request ID gets old fast once you have more than a couple of hosts. That’s a genuinely different problem — log aggregation and retention as a system rather than a per-file setting — and it’s worth reading about how journald’s own retention and vacuuming rules work before you decide whether you need a bigger tool on top of both.

Is it worth setting up

For anything outside what your distro’s packages already handle, yes, immediately — it’s a five-minute stanza and it closes off one of the most common ways a small server dies quietly over a long weekend. The mental model to keep is simple: logrotate runs once a day, moves and possibly compresses files by name, and if the application holds the file open by descriptor you must tell it to reopen or you have not actually freed anything. Get that one detail right and the rest of the tool is about as low-maintenance as infrastructure gets — write the rule once, and it is still quietly doing its job years later without you thinking about it again.

Smarc
Written by Smarc

Founder and editor of Aside House. A lifelong tinkerer who self-hosts far more than is sensible, hardens Linux boxes for fun, and prods the latest AI tools to see what they can really do. The how-to guides here are the notes Smarc wishes had existed the first time round.

Smarc is the editor of Aside House. The homelab, the hardware and the opinions are real; the guides are drafted with AI assistance. Infrastructure details are genericised on purpose.