DevOps Bootcamp · Lesson

Automated Log Rotation & Cleanup

Keep disks healthy by automating log rotation and old-file cleanup with logrotate and scheduled find-based scripts.

Lesson 4 of 413 steps

Automated Log Rotation & Cleanup is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

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.conf

A 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:

  • daily rotate every day
  • rotate 7 keep 7 old files
  • compress gzip rotated logs
  • missingok skip 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/myapp

postrotate 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 -delete

A 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>&1

Scheduling 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.sh

Monitoring 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 root

Quick 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 -delete for custom cleanup
  • Schedule with cron and guard with disk-usage alerts
Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “Automated Log Rotation & Cleanup” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Automated Log Rotation & Cleanup”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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

  1. Scheduling Tasks with Cron
  2. Automating System Administration
  3. Advanced Scripting Project
  4. Automated Log Rotation & Cleanup
← Back to DevOps Bootcamp