{ "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.
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.
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": "| Directive | Meaning |
|---|---|
daily/weekly/monthly | Rotation frequency |
rotate N | Keep N old copies |
compress | gzip old files |
delaycompress | Skip compressing the most recent rotation (useful when the app still has it open) |
missingok | Do not error if the log file does not exist |
notifempty | Skip rotation if the file is empty |
size 100M | Rotate when file exceeds this size instead of on schedule |