Interview Prep/Shell Scripting/Scenarios: backups, log rotation and housekeeping

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 →

  1. If the app supports reopening on a signal (nginx USR1, many daemons on HUP), use create plus postrotate:
    /var/log/nginx/*.log {
        daily
        rotate 14
        compress
        delaycompress
        sharedscripts
        postrotate
            [ -s /run/nginx.pid ] && kill -USR1 "$(cat /run/nginx.pid)"
        endscript
    }
    
  2. 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.
  3. 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.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: backups, log rotation and housekeeping