>_ IzzyLab

Hands-on task

This task is step 11 of the course “Disks, links and free space”

Free up space by truncating a log

easy~2 min

Logs have filled up the disk: /var/log/nginx/access.log has ballooned to 20 MiB, and nginx keeps writing to it — the server hosts a site on port 80. You can't delete the file — nginx holds it open and would keep writing into the "deleted" inode, so the space wouldn't be freed. Truncate the file in place, keeping the file itself: the site must keep working, and nginx must keep writing to the same log.

What you'll practise:
  • truncate
  • in-place truncation
  • open descriptor
  • freeing space
  • inode

Topic breakdown

In Unix, "deleting a file" and "freeing space" are not the same thing. As long as a live process holds the file open, its inode and blocks live on even after rm: the name disappears from the directory, but the process keeps writing to the already-"deleted" file, and the space doesn't come back until it exits. That's why you don't delete an active log — you truncate it in place, shortening its length to zero while keeping the file itself, its inode and the open descriptor. The blocks are freed immediately, and the service keeps writing to the same file (a writer that opened it in append mode continues from the new end; otherwise it keeps its old offset and the file becomes sparse).

What's checked

  1. log is in place
  2. log emptied
  3. the site responds, and nginx writes to this same file

What it looks like