Rotating logs without losing lines
Log rotation looks trivial until a service keeps writing into a file that no longer has a name. The right setup depends on how the program opens its log.
Why renaming is not enough
A process writes to an open file descriptor, not to a path. When logrotate renames app.log to app.log.1, the process keeps writing into what is now app.log.1. When that file is eventually deleted, its space is not released until the process closes it.
Two strategies
With create, the default, logrotate renames the file and creates a new empty one. The program then has to be told to reopen its logs, usually from a postrotate script:
/var/log/app/*.log {
weekly
rotate 8
compress
delaycompress
missingok
notifempty
sharedscripts
postrotate
systemctl kill --signal=SIGHUP app.service
endscript
}
delaycompress leaves the most recent rotated file uncompressed, which matters when the program takes a moment to reopen and still writes a few lines into it.
With copytruncate, logrotate copies the file and then truncates the original in place. The program needs no signal, but anything written between the copy and the truncation is lost, and a program that did not open the file with O_APPEND keeps writing at its old offset, which leaves a sparse file that starts with zero bytes.
Finding space held by deleted files
lsof +L1
lists open files whose link count is zero, that is, files deleted while still open. Restarting or signalling the process that holds them releases the space.
journald
A service that logs to standard output under systemd ends up in the journal, which rotates itself. Its size is limited by SystemMaxUse= in /etc/systemd/journald.conf, by default ten percent of the filesystem and at most 4 GiB, and journalctl --vacuum-size=500M trims it on demand. For new services this is usually simpler than writing log files at all.