Automated Log Rotation & Cleanup
Keep disks healthy by automating log rotation and old-file cleanup with logrotate and scheduled find-based scripts.
Automated Log Rotation & Cleanup is a free DevOps Bootcamp lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Problem of Growing Logs
Long-running services write logs forever. Without management they fill the disk and crash the system. Rotation and cleanup keep them in check.
du -sh /var/log/*What Log Rotation Does
Rotation renames the current log, starts a fresh empty one, and keeps a limited number of older compressed copies.
- app.log becomes app.log.1
- old copies get gzipped
- the oldest are deleted
Introducing logrotate
logrotate is the standard Linux tool that automates rotation based on size or age, defined under /etc/logrotate.d/.
cat /etc/logrotate.confA Basic logrotate Config
A config block names the log files and the rotation policy.
/var/log/myapp/*.log {
daily
rotate 7
compress
missingok
notifempty
}Understanding the Directives
Common directives:
dailyrotate every dayrotate 7keep 7 old filescompressgzip rotated logsmissingokskip if absent
Testing Without Waiting
Force a run with -f or do a dry run with -d to validate config before trusting the schedule.
logrotate -d /etc/logrotate.d/myapp
logrotate -f /etc/logrotate.d/myapppostrotate Scripts
Many services keep the old file handle open. A postrotate block signals the service to reopen its log after rotation.
/var/log/myapp/*.log {
daily
postrotate
systemctl reload myapp
endscript
}Cleaning Old Files with find
For ad-hoc cleanup, find with -mtime locates files older than N days and deletes them.
find /var/log/myapp -name "*.log" -mtime +30 -deleteA Safe Cleanup Script
Wrap cleanup in a script that logs what it removes, so you have an audit trail.
#!/bin/bash
DIR=/var/backups
find "$DIR" -name "*.tar.gz" -mtime +14 -print -delete \
>> /var/log/cleanup.log 2>&1Scheduling the Cleanup
Place rotation under logrotate (already cron-driven) and schedule custom cleanup scripts with your own crontab entry.
# crontab -e
30 2 * * * /usr/local/bin/cleanup.shMonitoring Disk Usage
Add a guard that alerts when a partition crosses a threshold so cleanup never runs too late.
USAGE=$(df --output=pcent / | tr -dc "0-9")
[ "$USAGE" -gt 90 ] && echo "Disk over 90%" | mail -s alert rootQuick Check
Test your automation knowledge.
Recap
You automated log hygiene:
- logrotate rotates by age/size with
rotate,compress,postrotate - Validate with
-d/-f - Use
find -mtime +N -deletefor custom cleanup - Schedule with cron and guard with disk-usage alerts
Frequently asked questions
Is the “Automated Log Rotation & Cleanup” lesson free?
Yes — the full text of “Automated Log Rotation & Cleanup” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Automated Log Rotation & Cleanup”?
Keep disks healthy by automating log rotation and old-file cleanup with logrotate and scheduled find-based scripts. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Automated Log Rotation & Cleanup” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Scheduling Tasks with Cron
- Automating System Administration
- Advanced Scripting Project
- Automated Log Rotation & Cleanup