Sysadmin Measured

Logrotate copytruncate vs create: Measured What Happens to a Live Writer

Every logrotate guide states the rule. This tutorial runs a live writer across a rotation and counts what lands where: 0 lines in the new file under create, 50 lines lost under copytruncate, and a dateext refusal quoted word for word.

Written November 8, 2026 · 12 min read

By the end of this guide
  • Case A: rename and create, writer holds its file open
  • Case B: copytruncate, same live writer
  • Case C: dateext, two forced rotations on the same day
  • Case E: the dateformat fix, executed
What you need
A Python writer opens the log once and holds the file descriptor open for 8 seconds, the way a real daemon does. It writes one numbered line every millisecond, about 886 lines per second, and records the first and last sequence number it wrote.

What this tutorial measures

Logrotate offers two ways to rotate a log that a program is still writing to. create renames the old file and makes a fresh one. copytruncate copies the old file, then empties it in place so the writer never has to reopen anything. The guides all state the trade-off: create breaks writers that do not reopen, copytruncate loses lines written between the copy and the truncate. None of them run a live writer across a rotation and count what lands where. This tutorial does that, on logrotate 3.21.0, with a writer that holds its file open for 8 seconds and numbers every line it writes.

The headline numbers:

Case What happened
A: rename + create 7,087 writer lines, 0 in the new file — all 7,087 went to the rotated file
B: copytruncate 7,090 writer lines, 50 lost — one consecutive run, in neither file
C: dateext, twice in one day Second rotation refused, quoted verbatim
D: second run, no -f Skipped by the state file, quoted verbatim
E: dateext + dateformat -%s Second rotation succeeds, two distinct destination names
F: create + postrotate SIGHUP Writer reopens, 4,436 lines in the new file, 0 lost

The setup

A Python writer opens the log once and holds the file descriptor open for 8 seconds, the way a real daemon does. It writes one numbered line every millisecond, about 886 lines per second, and records the first and last sequence number it wrote. Each case starts with a 200,000-line prefill, about 12 MB. The rotation is forced at 3 seconds in with:

Bash
logrotate -f -v -s <probe-dir>/state <config>

The audit compares the union of the rotated file and the live file against the full written range, sequence number by sequence number. A per-file line count alone cannot see the copytruncate loss, because the lost lines are in neither file. All state and config files live in the probe directory; nothing under /etc or /var is touched. The box man pages, logrotate.conf(5) and logrotate(8), were read fresh for every directive claim below.

Case A: rename and create, writer holds its file open

The config:

Config
/app.log {
    daily
    rotate 3
    missingok
    create 0644 alex alex
}

The writer wrote 7,087 lines, sequence numbers 200001 to 207087. After the rotation:

File Lines First seq Last seq
New app.log 0 — —
Rotated app.log.1 207,087 1 207,087

Every line written after the rotation went to the rotated file, not the live one. The new file is empty, 0 bytes, and stays empty until the writer reopens it. The mechanism is the file descriptor: the writer opened app.log once, and the rename changed the name, not the inode the descriptor points at. Logrotate did its part — the log shows creating new .../app.log mode = 0644 — and the writer still wrote to the old inode.

The logrotate man page states the create rule plainly: "Immediately after rotation (before the postrotate script is run) the log file is created (with the same name as the log file just rotated)." Creating the file is not the same as the writer using it. That gap is the whole trap, and here it is 7,087 out of 7,087 lines misdirected. A repeat run the day before measured the same shape with 7,104 writer lines and 0 in the new file, so this is not a one-run fluke.

The failure mode gets worse than an empty new file. If the next rotation cycle deletes the rotated file while the writer still holds it open, the writer keeps appending to an inode with no directory entry at all. The disk space stays allocated and invisible to ls, which is the incident Bubble documents with a 34 GB orphaned inode.

Case B: copytruncate, same live writer

The config changes by one directive:

Config
/app.log {
    daily
    rotate 3
    missingok
    copytruncate
}

The writer wrote 7,090 lines, sequence numbers 200001 to 207090. The audit:

File Lines First seq Last seq
Rotated app.log.1 202,656 1 202,656
Live app.log 4,384 202,707 207,090

Expected sequence numbers 1 to 207,090: 207,090 lines. Seen across both files: 207,040. Missing: 50 lines, sequence numbers 202657 to 202706, one consecutive run, in neither file. The rotated file ends at 202656 and the live file resumes at 202707. After the window, zero further loss: all 4,384 post-window lines are in the live file.

At 886 lines per second, 50 lines is a window of about 56 milliseconds (50 / 886). That window is derived from the line count and the measured write rate, not directly timed. The man page warns about exactly this: "Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost." The copy step reads a 12 MB file, so the window scales with file size and write rate. A repeat run the day before, same size and rate, lost 47 lines in about 53 milliseconds — same shape, different count, which is the point: the loss is per-run, not a constant.

One earlier measured result is worth naming. James Stanley ran a C program writing as fast as it could and lost 4 million lines across a rotation, which shows the same race at a much higher write rate. His test covers copytruncate alone, with no write rate or file size stated. This tutorial adds the other side: the same writer, same rate, same file size under create, plus the exact lost sequence range and the dateext cases below.

Case C: dateext, two forced rotations on the same day

With dateext, rotated files get a date suffix instead of a number:

Config
/app.log {
    daily
    rotate 3
    missingok
    dateext
    create 0644 alex alex
}

The first forced rotation renames app.log to app.log-20261108 and creates a new app.log. The second forced rotation the same day is refused, verbatim:

Output
error: destination .../app.log-20261108 already exists, skipping rotation

The second rotation's content, the line "second line", stays in the live log, not rotated. dateext with the default date format does not overwrite the existing file and does not pick a new name. It skips the rotation and says so.

Case E: the dateformat fix, executed

The fix is to give the date suffix finer granularity. Adding dateformat -%s puts the Unix epoch in the name instead of the calendar date:

Config
/app.log {
    daily
    rotate 3
    missingok
    dateext
    dateformat -%s
    create 0644 alex alex
}

Two forced rotations the same day, two seconds apart, both succeed with distinct destinations:

Rotation Destination Content
First app.log-1794133276 line one
Second app.log-1794133278 second line

The man page allows only %Y %m %d %H %M %S %V and %s in dateformat, and notes the system clock must be past September 9, 2001 for %s to work. A %Y%m%d-%H format fixes the same-day collision too, and keeps the name readable; %s fixes it down to the second. Two rotations inside one second would still collide under %s, which the guide does not claim to have tested.

Case D: the state file skips the second run

Running logrotate again without -f after a rotation today prints, verbatim:

Output
log does not need rotating (log has already been rotated)

The state file gates the second run even though the log has new content. The -f flag forces rotation "even if it doesn't think this is necessary", which is why the forced cases above rotate and the unforced run does not. If a rotation seems to do nothing, check the state file before suspecting the config.

Case F: the SIGHUP fix, executed

The fix for Case A is a postrotate script that tells the writer to reopen its log file. A second writer was run that installs a SIGHUP handler closing and reopening the log, with this config:

Config
/app.log {
    daily
    rotate 3
    missingok
    create 0644 alex alex
    postrotate
        kill -HUP $(cat writer.pid)
    endscript
}

The writer wrote 7,094 lines, sequence numbers 200001 to 207094, and reopened exactly once. After the rotation:

File Lines First seq Last seq
New app.log 4,436 202,659 207,094
Rotated app.log.1 202,658 1 202,658

The union is complete: 202,658 + 4,436 = 207,094, zero lines lost, zero lines misdirected. The new file starts at 202659, the first line written after the signal. create plus a working reopen is the no-loss configuration.

One caveat, from Bubble: the signal only works if the daemon actually reopens its log file on that signal. Some daemons treat SIGHUP as "reload config" without touching log handles, and some ignore it. Check the specific daemon's signal handling before trusting a postrotate block; the config above was executed against a writer that reopens, and the reopen count of 1 is part of the measurement.

Which one to use

Situation Use Why, measured
Daemon reopens on a signal create + postrotate SIGHUP 0 lost, 0 misdirected (Case F)
Daemon cannot be told to reopen copytruncate Writer keeps writing, but budget for the loss window (Case B: 50 lines at 886 lines/s on a 12 MB file)
Rotating twice a day with dateext Add dateformat with time in it Default date suffix collides and the rotation is skipped (Cases C and E)
Quiet log, low write rate Either A quiet log may lose 0 lines under copytruncate; the window only bites while lines are being written

The decision is about the writer, not the directive. If the writer reopens, create wins outright. If it cannot reopen, copytruncate is the workable choice and the loss window is the price, sized by file size times write rate.

What this does not cover

  • The 50-line loss is one run at one file size (about 12 MB) and one write rate (about 886 lines per second). The 56 ms window is derived from the count and the rate, not directly timed. A bigger file takes longer to copy, so the window grows; a quiet log may lose 0 lines.
  • The write rate is steady, one line per millisecond. A writer that writes in bursts loses a burst if it lands in the window, and loses nothing if it lands outside it.
  • Case A used a writer that never reopens. A daemon with a postrotate SIGHUP does not hit the Case A trap at all, as Case F shows.
  • The dateformat %s fix was tested with rotations two seconds apart. Two rotations inside one second would still collide, and that was not tested.
  • compress, delaycompress, sharedscripts, and su were not part of this probe. Nothing here claims anything about them.
  • Production configs live under /etc/logrotate.d and run from cron or a systemd timer; the OnCalendar validator checks the timer side of that schedule.

FAQ

Does copytruncate always lose lines?

No. It loses lines written between the copy finishing and the truncate landing. A log that writes nothing in that window loses nothing. A busy log loses a slice on every rotation, sized by file size and write rate.

Is create always wrong for daemons that do not reopen?

It is wrong for the live file, in the measured sense: 0 of 7,087 lines reached the new file. But create is the right directive once the daemon reopens, as Case F shows. The fix is the signal, not a different directive.

Why not just use copytruncate everywhere?

Because the loss is silent and it lands exactly when the log is busiest, which is when the log matters most for incident work. If the daemon reopens, create plus postrotate loses nothing and costs no copy.

What if my daemon ignores SIGHUP?

Then copytruncate is the honest choice, or the daemon's own reopen mechanism — a reload command, a control socket, a restart — goes in postrotate instead. Do not assume a signal reopens logs; verify it against the daemon's documentation.

Does the dateext collision matter if I rotate once a day?

Only if a rotation ever runs twice in one day: a manual -f run, a retried cron job, or a timer firing twice. The first rotation succeeds and the second is skipped silently enough that the content stays in the live log.

Sources

Summary
  • Under create, a writer that never reopens writes 0 lines to the new file. All 7,087 post-rotation lines went to the rotated file.
  • Under copytruncate, the loss is one consecutive run of lines, 50 here, in neither file. The window is about 56 ms at this size and rate.
  • dateext refuses a second same-day rotation with the default date format. dateformat -%s makes the second rotation succeed with a distinct name.
  • A run without -f after a rotation today is skipped by the state file, even with new content in the log.
  • create plus a postrotate SIGHUP that the daemon actually honors loses nothing: 4,436 lines in the new file, 0 lost.

Keep Exploring