Course contentsShow
← Linux
Lesson 225 of 1,8192. Deep Dive into Bash ScriptingFree lesson

Log Rotator Implementation

Build efficient systems to prevent log overflow in long-running jobs.

In this lesson, you will learn how to prevent your long-running scripts from crashing your system by implementing a custom log rotation mechanism.

When a background process writes to a log file indefinitely, it will eventually consume all available disk space, leading to a system-wide failure. A "Log Rotator" is a system that periodically renames the current log file and starts a fresh one, ensuring your scripts stay healthy without manual intervention.

The Logic of Rotation

To implement a manual rotator, we follow three steps:

  1. Check size: Determine if the current file has exceeded a defined threshold.
  2. Archive: Rename the current log to include a timestamp or version number.
  3. Clean: Delete older archives that are no longer needed to keep your storage footprint predictable.

Here is a basic implementation of this logic:

#!/bin/bash
LOG_FILE="app.log"
MAX_SIZE=1024 # Limit in KB

# Check if file size exceeds threshold
if [ $(du -k "$LOG_FILE" | cut -f1) -gt $MAX_SIZE ]; then
    # Archive current log with a timestamp
    mv "$LOG_FILE" "${LOG_FILE}.$(date +%Y%m%d%H%M%S).old"
    
    # Create a fresh log file
    touch "$LOG_FILE"
    
    # Optional: Delete logs older than 7 days to manage disk space
    find . -name "app.log.*.old" -mtime +7 -delete
fi

Why Manual Implementation?

While Linux provides a system-wide logrotate utility, building your own logic into your script is a powerful way to ensure that your specific application handles its own lifecycle. This is particularly useful for scripts running in isolated containers or environments where you don't want to rely on external system configurations.

By adding this snippet to your main execution loop or calling it via a scheduled check, you turn a potential "disk space bomb" into a self-maintaining, robust system. Always ensure your script has the necessary permissions to move and delete files in the directory where your logs reside.

Key Takeaway: Log rotation is a critical defense mechanism that maintains system stability by capping file growth and managing disk consumption through archiving and periodic cleanup.