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

You deleted a 40 GB log file, but `df` still shows the disk full. What happened and how do you script the fix?

IntermediateScenarioScenarios: backups, log rotation and housekeeping
Answer

Situation read → A process still holds the file open; the inode and its blocks are freed only when the last descriptor closes.

What I'd do →

  1. Find deleted-but-open files: lsof +L1 or find /proc/*/fd -lname '*(deleted)' 2>/dev/null.
  2. Truncate through the process's file descriptor without restarting it:
    lsof -nP +L1 2>/dev/null | awk 'NR > 1 && $7 > 1e9 {print $2, $4}' |
    while read -r pid fd; do
      fd=${fd%[rwu]}
      : > "/proc/$pid/fd/$fd"
      echo "truncated pid $pid fd $fd"
    done
    
  3. Or restart/reload the service so it reopens its logs.
  4. Fix rotation so the app is signalled or copytruncate is used.

Why → rm removes the name, not the data, while a process holds it.

Interview tip → Mention that du and df disagreeing is the tell-tale sign of deleted open files.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: backups, log rotation and housekeeping