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

`/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?

BeginnerScenarioScenarios: backups, log rotation and housekeeping
Answer

Situation read → Two jobs: free space safely right now, then make sure it cannot happen again.

What I'd do →

  1. Find the culprit: du -xh --max-depth=2 /var 2>/dev/null | sort -h | tail.
  2. If the app holds the file open, truncate it instead of deleting it: : > /var/log/app/app.log (or compress a copy first if you need it).
  3. Compress or delete old rotated files: find /var/log/app -name '*.log.*' -mtime +3 -delete.
  4. Check for deleted-but-open files: lsof +L1.
  5. Add /etc/logrotate.d/app:
    /var/log/app/*.log {
        daily
        rotate 14
        compress
        delaycompress
        missingok
        notifempty
        copytruncate
    }
    
  6. Test with logrotate -d /etc/logrotate.d/app (debug, no changes), then logrotate -f once.
  7. Add a disk-usage alert at 80% and 90%.

Why → Deleting an open log frees no space until the process closes it; truncation does.

Interview tip → Mention logrotate -d for a dry run. It shows you test config before applying it.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: backups, log rotation and housekeeping