Rotating a log changes the filenames on disk. The application must also switch where it writes. On Linux, a process with an open descriptor can keep writing to the same file after a rename. If that file is later removed while still open, its storage can remain allocated.
This reference lab targets Debian or Ubuntu with packaged NGINX, logrotate, Bash, curl, and GNU Coreutils on Linux. Run it as an ordinary user with those tools installed. It uses a unique temporary directory, a private PID file, and loopback port 18082. The existing NGINX service and system logrotate configuration are outside the lab.
| Stage | Current pathname | Where NGINX writes |
|---|---|---|
| Before rotation | access.log | Original file |
| After rename and creation | access.log.1 and a new access.log | Original file until reopen |
| After successful reopen | access.log.1 and a new access.log | New access.log |
1. Start a disposable NGINX instance
Check that port 18082 is available before continuing. Keep the following commands in the same shell.
|
|
Stop if either NGINX command fails. With a successful start, confirm that an HTTP request appears in the initial access log.
|
|
2. Configure rotation with a reopen hook
NGINX documents USR1 as its log-reopen signal. The nginx -s reopen command uses this behavior and reads the PID from the configuration supplied here. Use the exact same prefix and configuration as the lab start.
|
|
Debug mode shows what the forced lab run would do without updating logs or state. Review the paths before performing the rotation. The lab has its own state file, so the command does not reuse the host scheduler’s state.
|
|
Expect two different inode numbers, with the original inode now at access.log.1. That confirms the rename and creation steps. Signal delivery and log routing still need their own check.
3. Verify where a fresh request was written
Signal handling can be asynchronous. Give the workers a bounded interval to reopen, then inspect both logs.
|
|
The expected result is the fresh marker in the current log and the initial request in the archive. A request sent during the transition may still land in the archive. If new requests keep landing there, inspect the lab error log, the PID file, and permissions on the replacement file and its parent directory.
4. Adapt the approach to an existing service
Inspect the distribution’s existing logrotate rule and service unit first. A packaged NGINX instance may already have a reopen hook. Add a second rule only for a distinct log path. Duplicate rules for the same path can break rotation.
Use the service’s documented reopen method and a scoped process identity. Avoid broad signals such as killing or signaling every process with the same name. Match the replacement file’s ownership and mode to the service. Test one rotation and a fresh client request before enabling compression or changing retention.
For an application without a reopen mechanism, logrotate’s copytruncate copies and then truncates the original file. The same descriptor can continue to work, but writes between copy and truncate can be lost. Evaluate that tradeoff against the application’s own logging options and evidence requirements.
If storage remains allocated after log deletion, use the deleted-but-open-file guide. For systemd journal files, use journald’s own retention controls.
5. Stop and remove the lab
Send a graceful stop to the lab instance. Wait for the process to exit before removing its directory. Retain the logs until you have recorded the results.
|
|
Once the lab process has exited, confirm that lab still points to the directory created by this article before removing it. For a production rollback, restore the prior rotation rule and verify that a fresh request reaches the current log. Keep rotated files available for investigation.