Infrastructure
Linux: diagnose log rotation before forcing logrotate
A production runbook for finding a file still open after rotation, protecting log collection and validating a reversible fix.
A service is still producing events, but its active log file stays empty after rotation. The old file keeps growing, the collector stops receiving new lines and disk usage rises. Forcing another rotation can hide the symptom, remove a useful archive or rerun a service script without fixing the open file descriptor.
In this fictional case, an application writes to /var/log/export/worker.log on a Linux VM. logrotate manages rotation and a collector forwards the logs. The goal is to restore a verifiable stream, not to claim that already lost events can be recovered.
Contain and identify the files
Notify the service and collection owners. Record the host, UTC incident window, free blocks and inodes, expected path, collection lag and recent changes. Preserve archives needed for the investigation and retention obligations; do not delete them for space without an explicit decision. If disk pressure threatens the service, follow the approved emergency procedure and monitor the business outcome too.
Adapt these read-only commands to local names and permissions. Their output may contain sensitive information.
date -u
df -h /var/log/export
df -i /var/log/export
sudo ls -li /var/log/export/worker.log*
sudo lsof -nP -- /var/log/export/worker.log*
sudo lsof -nP +L1
systemctl status logrotate.timer logrotate.service --no-pager
journalctl -u logrotate.service --since "24 hours ago" --no-pager
Compare inode numbers and file sizes over time, and identify the producer PID and descriptor. A renamed or deleted file that remains open can still receive writes. Removing its name does not free its blocks while a process holds it open. Check whether the collector reads the old inode, the new path or neither. Missing systemd entries do not prove rotation never ran: the distribution may use cron or another scheduler.
Explain the rotation decision
Inspect the global configuration, includes, applicable rule, scripts and service or timer overrides. Record the logrotate version, the state file actually used and the scheduler’s last result. Paths and read permissions differ by distribution.
logrotate --version
systemctl cat logrotate.timer logrotate.service
sudo grep -nE 'include|worker.log|copytruncate|postrotate|size|daily' /etc/logrotate.conf /etc/logrotate.d/*
sudo logrotate --debug /etc/logrotate.conf
Debug mode explains decisions without rotating files or updating state. It does not prove a real run will succeed: permissions, space, scripts and concurrency still need checking. Read the full configuration, not only the filtered lines. If the service uses another configuration or state file, reproduce its context read-only and check options supported by the installed version.
| Observation | Next evidence |
|---|---|
| Old inode is open and growing | Producer PID, supported reopen mechanism and postrotate script result |
| New file receives writes but downstream is empty | Collector position, errors and rotation handling |
| Debug predicts no rotation | Actual schedule, threshold, state and effective configuration |
| Rotation failed | Logs, permissions, space, script and state lock |
A size-based rule is evaluated only when logrotate runs, so check the scheduler frequency. A failed run can still update state; do not delete it to force another attempt. Do not bypass its lock to run parallel rotations against the same files.
Repair one stream
If the application supports reopening its log, prefer its documented mechanism and verify the associated script’s result. Do not assume a generic signal is safe: confirm its semantics and impact with the owner. Rename-based rotation requires the producer to open the new file; check ownership, permissions and security context too.
If copying and then truncating is the only viable option, agree on the trade-off with the owners: lines can be lost between copy and truncation, and collector behavior must be tested. It is not a universal fix. The official logrotate documentation describes these options and their limits.
Prepare a versioned change, its scope, required disk headroom, permissions, retention and rollback. First test on a representative host or copy with isolated configuration, state file and test logs. Do not force the global production configuration. An isolated run alone does not prove the behavior of the real service and collector.
Validate and decide
On an approved canary, emit identifiable test lines before and after rotation through the application’s normal mechanism. Check inodes and sizes, reopening by the correct PID, permissions, rotation results and errors. Find the markers both in the expected file and at the collection destination, allowing for pipeline latency; check gaps and duplicates against the pipeline’s guarantees. Then observe a normal scheduled rotation and disk usage.
If the stream is still missing or the service degrades, stop the rollout, restore the previous versioned configuration and reload it under the local procedure. Recheck both producer and collector: configuration rollback cannot restore lost lines or deleted archives. Preserve evidence, involve the owners and keep an approved alternative collection path if needed. Expand only after reopening, collection and retention are demonstrated.