NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Middle0 views

What should you do if you find an active log file that is still being written to? Why can't you simply delete it?

Practice

Asked at

AI answer

First, find out which process writes to the file and how the application reopens logs. Deleting an active file usually removes only its name from the directory: the process continues writing to the open file descriptor, so disk space is not freed until the descriptor is closed. Meanwhile, the log is no longer accessible through its previous path, and its data may be lost when the process restarts.

Use log rotation: rename the file, create a new one, and ask the application to reopen the log (for example, with a signal or command). If reopening is not possible, copytruncate may be a temporary option, but copying can result in lost or duplicated entries. On Linux, you can find a process holding a deleted file, for example, with lsof +L1.