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 →
- Find deleted-but-open files:
lsof +L1orfind /proc/*/fd -lname '*(deleted)' 2>/dev/null. - 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 - Or restart/reload the service so it reopens its logs.
- Fix rotation so the app is signalled or
copytruncateis 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.
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?BeginnerShould you use `copytruncate` or a `postrotate` signal for an application's logs? How do you decide?Intermediate`/srv/app/releases` has 300 old release directories. Write a cleanup that keeps the newest 5 and never deletes the current release.Beginner