What Is Logrotate?
Every Linux distribution ships something for log rotation, and logrotate is almost always the something. It renames the file an application is writing to, opens an empty one under the old name, and deletes the oldest copy on the way past.
The numbering runs backwards from now. app.log.1 holds the most recent rotation, and app.log.7 is the one about to disappear.
Compression and deletion are part of the same run. Most distros install it as a dependency, so it's usually on the host already.
Development's settled down. Release 3.22.0 landed in June 2024 and nothing has shipped since, which on a utility this old reads as stability rather than neglect.
One thing about it catches people out. Nothing gets watched continuously, and files get read only when something wakes the binary up.
What Runs Logrotate on a Schedule?
A timer or a cron job does. The run is brief, exiting as soon as it has rotated whatever qualified.
What wakes it | Where that lives | How often it checks |
systemd timer | logrotate.timer, calling logrotate.service | Once a day, at a moment systemd staggers across hosts |
Daily cron script | /etc/cron.daily/logrotate | Once a day, whenever cron.daily happens to run |
Hourly cron script | /etc/cron.hourly/logrotate | Twenty-four times, which hourly rotation actually needs |
Current Debian, Ubuntu, RHEL, Fedora, SUSE and Arch all use the timer now, though most of the guidance online still describes the cron script. Both end up calling the same binary.
Whichever one you've got sets a floor nothing in your config can get under. A file marked hourly still rotates once a day if a daily timer is the only thing calling logrotate.
Moving the script into /etc/cron.hourly raises the floor on a cron host. Under systemd the timer gets overridden instead (3.22.0 also spreads the start times out, so a whole fleet doesn't rotate in the same second).
Where Does Logrotate Read Its Configuration?
Two places, and nearly everything actually lives in the second one. Defaults sit in /etc/logrotate.conf, while /etc/logrotate.d gets a file per application.
Almost nobody writes those per-application files by hand. Package managers drop them in as services get installed, so a host you've never touched already rotates its logs.
Reading the two in the wrong order is the usual mistake. A directive aimed at a specific path wins, and one left out inherits the global default instead.
So a block that looks like it rotates on no schedule at all is probably rotating weekly, because that's what the global file said and nobody overrode it.
Ubuntu's own defaults are weekly, four files kept, and compress left commented out. Roughly a month of history then sits on the disk uncompressed until somebody notices.
The syslog daemon's file usually owns everything under /var/log. An application logging somewhere else brings its own rules, or none at all, and that second case fills disks.
What Goes Inside a Logrotate Config Block?
The shape is a path, then directives inside braces. Globs work in the path, and several paths can share one block if spaces separate them.
Directive | What it does |
rotate 14 | Keeps fourteen old files and deletes the one that falls off the end |
daily, weekly, monthly, yearly | How often, for the files where the clock decides rather than the size |
compress | Runs gzip over rotated files, which on plain text takes most of the size away |
delaycompress | Leaves the newest rotated file uncompressed until the following run |
missingok | Passes over a file that isn't there instead of reporting an error |
notifempty | Passes over a file with nothing written to it |
create 0640 root adm | Sets mode, owner and group on the replacement file |
dateext | Names rotated files by date rather than by number |
su root adm | Performs the rotation as that user and group |
sharedscripts | Runs postrotate once for the whole block rather than once per file |
Scripts hook into the run through prerotate and postrotate blocks, each closed with endscript. Nginx uses one to reopen its log files once rotation has finished (its shipped config does exactly that).
Without sharedscripts, that script runs once per file, which matters on a block covering both an access log and an error log. Nginx would be told to reopen twice in a row.
How Do You Rotate by Size Rather Than by Time?
The size directive covers it. A block carrying size 50M rotates its file once the file crosses fifty megabytes, whatever the calendar says.
Units start at bytes and run through k and M up to G (a bare number counts as bytes, which is hardly ever what anyone meant).
Size and the interval directives can't both apply. Whichever one appears last in the block wins, and nothing warns you about the one that lost.
Two middle options exist for when neither suits on its own. minsize waits for the schedule and the size together, while maxsize rotates early if the file gets there first.
A busy proxy usually wants maxsize. Traffic spikes then get a file of their own rather than one enormous daily log.
None of this escapes the timer. A size trigger is only tested when logrotate runs, so a file can sit well over its limit until the next pass comes round.
Is create or copytruncate the Safer Choice?
Create, in almost every case. The choice matters because your application still holds the old file open.
Method | What happens to the open file | How it goes wrong |
create, the default | Renamed out from under the application, with a new empty file taking its name | Software that never reopens keeps writing into the renamed file, so the new one stays at zero bytes |
copytruncate | Copied, then emptied in place, so the application never loses its handle | Anything written between the copy and the truncate is gone for good |
Create doesn't lose anything as long as the application reopens its file, which most software manages when a postrotate script signals it.
Copytruncate exists for software that can't be made to reopen at all. Those lost lines aren't hypothetical, though the window lasts only as long as the copy.
A copy directive exists too, snapshotting the file and leaving the original to grow. That one archives a log mid-flight rather than managing it.
How Do You Test a Logrotate Config Before Trusting It?
Debug mode answers this. Nothing gets written, and the state file keeps its old contents, so a dry run against production costs you nothing.
What you want | What to run |
A dry run of one application's rules | logrotate --debug /etc/logrotate.d/app |
A dry run of everything | logrotate --debug /etc/logrotate.conf |
A real rotation now, conditions ignored | logrotate -vf /etc/logrotate.d/app |
A record of what each run did | logrotate --log /var/log/logrotate.log /etc/logrotate.conf |
Each skip comes with its reason attached, in wording like log does not need rotating or log does not exist.
The state file is the other place to look. Half the confusion about it comes down to the path (and -s moves the file anywhere you want).
Debian and Ubuntu put it at /var/lib/logrotate/status, while most of the rest drop the directory and use /var/lib/logrotate.status.
Every file logrotate manages shows up there with the date it last rotated. A file missing from that list never rotated, so usually no config covers it.
Two silent failures account for most of the rest. Insecure permissions on a parent directory make logrotate skip a file, until an su directive names a user to become.
The other is notifempty, which passes over anything with nothing written in it. A log that went quiet after a service stopped therefore sits at whatever size it reached.
Why Logrotate Is Not Log Management
Rotation protects the disk. It does nothing for the record, because logrotate deletes whatever falls off the end of the retained count.
That's fine where a copy already lives somewhere else. Log collection moving events off the host as they're written demotes the local file to a buffer.
A log forwarder usually gets them across. Rotation on the host can then run as hard as the disk needs.
Central storage also answers questions one host can't. Log monitoring searches every host at once, and it runs log retention on its own clock.
So the two settings answer different problems, and they're worth reading separately. A host with a tidy rotation policy and nothing forwarding off it gives you a week of history that you can only read one file at a time.
Check the forwarding before you shorten anything. Rotating harder only becomes safe once the copy that matters has left the host, and that's where Linux log management stops being a per-host chore.
Explore More IT Terms
Browse our comprehensive IT glossary to learn more about technology terminology.