Atomically Copy and Truncate a Log File on Linux
Copy then truncate leaves a gap where appended lines vanish. Rename closes it but only if the writer reopens its file. Here is what each workaround costs.

You have a log file that a process is appending to, one complete line per write call. You want to copy it to a second file and then empty the original, and you cannot afford to lose any line written in between. The writer holds the file open and cannot be told to close and reopen it. In the question this comes from, the writer is systemd, which has no close-and-reopen function for that log.
The obvious code is two steps: shutil.copyfile('foo', 'bar') followed by a truncate of foo. Those are two separate operations, and nothing makes them atomic with respect to the writer. The asker wants them to behave as one without relying on the writer closing the file.
This article walks through why the window exists, why rename only helps if the writer cooperates, and what the two workarounds from the question (stopping the writer, or interposing a FIFO) actually cost.
Where the window is
Copy and truncate are independent system-level actions. The copy reads the file up to the end it sees at that moment. The truncate then sets the length to zero. Any line the writer appends after the copy has read the end of the file, but before the truncate lands, is destroyed without ever having been copied.
Single-call, line-at-a-time writes keep the file consistent at every instant, as the asker notes. That prevents torn lines. It does nothing for this race, because the problem is not a half-written line but a whole line landing in the gap.
A smaller point in the snippet itself: os.ftruncate takes an open file descriptor, not a path. Passing the string 'foo' fails. You would use os.truncate('foo', 0) with a path, or open the file and pass its descriptor. Neither version changes the race.
Why rename moves the problem rather than solving it
The standard suggestion is to use rename() instead of copy plus truncate. Rename the active file to bar, then create a new foo. Rename is a single operation, so there is no gap in which data can be dropped between copying and emptying.
The catch is what the writer does next. A process holding an open descriptor keeps writing to the same inode, and the inode is now named bar. New lines keep landing in the file you just rotated, while the freshly created foo stays empty. Nothing is lost, but the log has not been rotated in any useful sense.
Rename therefore only works if the writer opens the log by path again after the rename. That is exactly the capability the asker says the writer lacks. As the answer puts it, if the writer must keep writing to the same open inode, copy plus truncate cannot be made truly atomic without coordinating with the writer.
Pausing the writer with SIGSTOP
The asker's first idea is to send SIGSTOP to the writer, copy, truncate, then send SIGCONT. With the writer stopped, no append can occur between the copy and the truncate, so the window closes.
The cost is that the writer is paused for the duration of the copy. For a small file that may be brief, but it scales with the size of the file you are copying. Anything the process does besides logging stops too. The asker's objection to this option is precisely that the writer is paused, and the answer agrees that a short pause is safer than risking lost lines. It is a judgement about how much stall you can tolerate versus how many lost lines.
Two things to check before using it: which process actually holds the file descriptor, and whether stopping that process is acceptable. If a signal-stopped process is also something other components depend on, a pause can cascade. The thread does not say how systemd behaves here, so test it rather than assume.
Putting a FIFO between the writer and the file
The second idea is to have the writer write to a FIFO instead of the log file. A separate process reads the FIFO and writes the real log, so it is free to close and reopen the destination whenever it likes. The FIFO's buffer is meant to absorb writes while the reader briefly handles the switch, so the writer does not block.
This removes the need for the writer to cooperate with rotation, but you are now running a relay process that must never fall behind. A FIFO buffer is finite. If the reader stalls for longer than the buffer can absorb, the writer blocks, which is the behaviour you were trying to avoid. If the reader dies, you have to decide what happens to the writer's output.
It also requires that the writer can be pointed at a FIFO at all. The question does not establish that systemd's log output can be redirected this way, so treat that as something to verify. The asker already calls this approach overcomplicated, and for a single log file that judgement seems fair.
Where this does not fit, and what to watch for
If you control the writer, the clean fix is to give it a reopen mechanism and use rename. That is the only option of the three that is genuinely atomic and does not pause anything. Everything else is a compromise you take on because you cannot change the writer.
- Copy then truncate, with the writer running: can lose lines in the window. Accept this only if lost lines are tolerable.
- SIGSTOP and SIGCONT: no lost lines, but the writer stalls for as long as the copy takes.
- FIFO relay: no stall in the normal case, but an extra process and a finite buffer to manage.
- Rename alone: atomic, but the writer keeps writing to the renamed file unless it reopens by path.
If the writer is a service you do not control and lines must never be lost, the honest conclusion from the answer is to use a proper log-rotation mechanism that coordinates with the writer, rather than trying to make copy and truncate atomic from the outside.
This article draws on Atomically copying and truncating a file being appended to and an answer to it, on the Stack Exchange network, licensed under CC BY-SA 4.0.
- #file-copy
- #filesystems
- #linux
- #log rotation
- #rename
- #truncate


Comments