Should you use `copytruncate` or a `postrotate` signal for an application's logs? How do you decide?
IntermediateScenarioScenarios: backups, log rotation and housekeeping
Answer
Situation read → The choice depends on whether the application can reopen its log file.
What I'd do →
- If the app supports reopening on a signal (nginx
USR1, many daemons onHUP), usecreatepluspostrotate:/var/log/nginx/*.log { daily rotate 14 compress delaycompress sharedscripts postrotate [ -s /run/nginx.pid ] && kill -USR1 "$(cat /run/nginx.pid)" endscript } - If it cannot reopen (some Java or Node apps writing to a file), use
copytruncate, accepting that lines written between the copy and the truncate can be lost. - Best of all for new services: log to stdout and let journald or the container runtime handle rotation.
Why → Rotating by rename without a reopen means the app keeps writing to the renamed (and later deleted) file, and disk usage never drops.
Interview tip → Explain why delaycompress exists: the app may still write to the .1 file briefly after rotation.
More on Scenarios: backups, log rotation and housekeeping
Design a nightly backup of `/etc` and application data to S3 with retention.IntermediateWrite the core of a Postgres backup that streams `pg_dump` through gzip to S3 and never uploads an empty dump.IntermediateBackups have "succeeded" every night for a year, but the first real restore failed. How do you make backups trustworthy?Advanced`/var` is 100% full because an app's logs were never rotated, and the app now fails to start. What do you do now, and what do you set up after?Beginner`/srv/app/releases` has 300 old release directories. Write a cleanup that keeps the newest 5 and never deletes the current release.BeginnerA Docker host keeps filling its disk. Write a safe cleanup approach.Beginner