>_ IzzyLab

Hands-on task

This task is step 14 of the course “Logs: analysis and housekeeping”

Truncate the log of a running export

easy~3 min

The report export is running right now and appends to /var/log/reports/export.log, which has grown to 30 MiB. You can't delete the file: the process holds it open, and the space won't come back. Truncate the log to zero in place — same file, same inode, the export keeps writing.

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

Topic breakdown

While a process holds a file open, rm only removes the name from the directory: the inode and its blocks live until the last descriptor is closed, and df won't show the space freed. That's why a live log is truncated in place: the file length becomes zero, the blocks are freed immediately, and the process's descriptor stays valid. If the writer opened the file in append mode (>>, O_APPEND), the next lines go to the start of the truncated file; without that flag it continues at the old offset and the file becomes sparse.

What's checked

  1. the log is in place, same inode
  2. the log is truncated

What it looks like