{ "id": "disk-logs", "title": "Disk Space & Log Rotation", "category": "storage", "tags": ["disk", "df", "du", "logs", "logrotate", "cleanup"], "updated": "2025-08-22", "summary": "Finding what is filling the disk and keeping logs from growing unbounded.", "sections": [ { "heading": "Checking Disk Usage", "body": "

df shows you how full each filesystem is. du tells you where the space went.

", "code": "df -h # human-readable filesystem summary\ndf -h /var/log # check a specific mount\n\ndu -sh /var/log/* # top-level breakdown of /var/log\ndu -sh /var/* | sort -rh # sort by size, largest first\ndu -sh /var/log/*.log # sizes of individual log files" }, { "heading": "Finding Large Files", "body": "

When du does not point at an obvious culprit:

", "code": "# Files over 100MB anywhere on the system\nfind / -xdev -size +100M -type f 2>/dev/null\n\n# Files in /var that have grown recently\nfind /var -xdev -mtime -1 -size +10M -type f 2>/dev/null" }, { "heading": "Emergency Cleanup", "body": "

If disk is at 100% and a service is failing because of it:

", "code": "# Truncate a log file without deleting it (safe for running processes)\ntruncate -s 0 /var/log/nginx/access.log\n\n# Remove old compressed logs (the .gz files are already rotated)\nrm /var/log/nginx/*.gz\n\n# Clear journald logs older than 2 days\njournalctl --vacuum-time=2d" }, { "heading": "logrotate Basics", "body": "

logrotate is the standard tool for rotating and compressing logs on a schedule. It is usually run daily from cron. Config files live in /etc/logrotate.d/—one file per service.

" }, { "heading": "Writing a logrotate Config", "body": "

Example for an nginx access log:

", "code": "/var/log/nginx/access.log {\n daily\n rotate 14\n compress\n delaycompress\n missingok\n notifempty\n sharedscripts\n postrotate\n /bin/kill -USR1 $(cat /run/nginx.pid 2>/dev/null) 2>/dev/null || true\n endscript\n}" }, { "heading": "Testing logrotate", "body": "

Run logrotate manually in debug mode to verify a config without actually rotating anything:

", "code": "logrotate -d /etc/logrotate.d/nginx\n\n# To force a rotation right now (useful for testing):\nlogrotate -f /etc/logrotate.d/nginx" }, { "heading": "Key logrotate Directives", "body": "
DirectiveMeaning
daily/weekly/monthlyRotation frequency
rotate NKeep N old copies
compressgzip old files
delaycompressSkip compressing the most recent rotation (useful when the app still has it open)
missingokDo not error if the log file does not exist
notifemptySkip rotation if the file is empty
size 100MRotate when file exceeds this size instead of on schedule
" } ] }