?
cypress
dart
java
+
java
bitbucket
d
+
dynamo
...
+
+
+
qwik
=>
alpine
+
+
โˆž
jquery
+
riot
+
+
+
+
+
haskell
angular
sql
+
+
+
dask
stencil
+
express
+
+
+
rubymine
+
ฯ€
aws
jwt
surrealdb
+
+
โ‰ˆ
+
+
+
+
nomad
windows
swift
alpine
+
+
laravel
weaviate
parcel
+
+
mint
+
+
[]
cdn
+
sklearn
java
chef
chef
chef
spring
+
next
+
+
โ‰ 
eslint
parcel
+
+
ios
+
f#
+
Back to Blog
๐Ÿ“œ Log Rotation and Management with logrotate in AlmaLinux: Clean Guide
AlmaLinux Log Management logrotate

๐Ÿ“œ Log Rotation and Management with logrotate in AlmaLinux: Clean Guide

Published Aug 20, 2025

Master log rotation in AlmaLinux using logrotate. Keep logs organized, save disk space, and automate log management with easy-to-follow examples for beginners.

10 min read
0 views
Table of Contents

๐Ÿ“œ Log Rotation and Management with logrotate in AlmaLinux: Clean Guide

Ever had your server crash because the disk filled up with logs? ๐Ÿ˜ฑ Yeah, me too! Itโ€™s embarrassing, right? One time, a simple Apache log ate up 50GB and brought down our whole web server. Thatโ€™s when I learned about logrotate - the unsung hero of Linux systems! Today Iโ€™m gonna show you how to keep your logs under control, save disk space, and never worry about runaway log files again. Letโ€™s tame those logs! ๐Ÿ“

๐Ÿค” Why is Log Rotation Important?

Logs are like rabbits - they multiply fast! Without proper management, theyโ€™ll eat all your disk space. Hereโ€™s why log rotation is absolutely essential:

  • ๐Ÿ’พ Save Disk Space - Prevent logs from filling your drive
  • ๐Ÿ” Keep Logs Organized - Find what you need quickly
  • ๐Ÿ“Š Maintain History - Keep old logs for compliance
  • ๐Ÿš€ Better Performance - Smaller logs = faster searches
  • ๐Ÿ”’ Security - Archive sensitive logs properly
  • ๐Ÿ’ฐ Save Money - No emergency disk upgrades!

Trust me, once you set up logrotate properly, youโ€™ll sleep better at night! ๐Ÿ˜ด

๐ŸŽฏ What You Need

Before we start rotating logs, check you have:

  • โœ… AlmaLinux system running
  • โœ… Root or sudo access
  • โœ… Some log files to manage
  • โœ… 15 minutes to organize your logs forever
  • โœ… Coffee (log management is serious business! โ˜•)

๐Ÿ“ Step 1: Understanding logrotate Basics

Logrotate is already installed on AlmaLinux - itโ€™s that important! Letโ€™s explore how it works.

Check logrotate Status

# Check if logrotate is installed
rpm -qa | grep logrotate

# See version
logrotate --version

# Main config file
cat /etc/logrotate.conf

# Config directory for apps
ls -la /etc/logrotate.d/

# Check when it last ran
cat /var/lib/logrotate/logrotate.status

How logrotate Works

# Logrotate runs daily via cron
cat /etc/cron.daily/logrotate

# Manual run (dry run - no changes)
logrotate -d /etc/logrotate.conf

# Force rotation (even if not due)
logrotate -f /etc/logrotate.conf

# Verbose mode to see what's happening
logrotate -v /etc/logrotate.conf

The rotation cycle: ๐Ÿ”„

  1. Check if logs need rotating (size/time)
  2. Rename current log (add number/date)
  3. Create new empty log file
  4. Compress old logs (optional)
  5. Delete really old logs

๐Ÿ”ง Step 2: Configuring Log Rotation

Letโ€™s create custom rotation rules! This is where the magic happens.

Basic Configuration

# Create config for your app
sudo nano /etc/logrotate.d/myapp

# Basic configuration
/var/log/myapp/*.log {
    daily                # Rotate daily
    rotate 7             # Keep 7 old logs
    compress             # Compress old logs
    delaycompress        # Compress on next rotation
    missingok            # Don't error if missing
    notifempty           # Don't rotate empty logs
    create 644 root root # Create new log with permissions
}

Advanced Configuration Options

# More sophisticated example
/var/log/myapp/*.log {
    # Rotation frequency
    daily               # or weekly, monthly, yearly
    
    # How many to keep
    rotate 30           # Keep 30 old files
    
    # Size-based rotation
    size 100M           # Rotate when reaches 100MB
    maxsize 500M        # Force rotate at 500MB
    
    # Compression
    compress            # Gzip old logs
    compresscmd /usr/bin/gzip
    compressext .gz
    delaycompress       # Don't compress most recent
    
    # Permissions
    create 640 appuser appgroup
    
    # Scripts
    sharedscripts       # Run scripts once for all logs
    postrotate
        # Commands to run after rotation
        systemctl reload myapp
    endscript
    
    prerotate
        # Commands before rotation
        echo "Rotating logs at $(date)" >> /var/log/rotation.log
    endscript
}

Date-Based Naming

# Use dates instead of numbers
/var/log/nginx/*.log {
    daily
    rotate 30
    dateext                  # Use date extension
    dateformat -%Y%m%d       # Format: -20250820
    dateyesterday           # Use yesterday's date
    compress
    create 644 nginx nginx
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

๐ŸŒŸ Step 3: Real-World Examples

Letโ€™s set up rotation for common services!

Apache/httpd Logs

sudo nano /etc/logrotate.d/httpd

/var/log/httpd/*log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 640 apache apache
    sharedscripts
    postrotate
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    endscript
}

Custom Application Logs

sudo nano /etc/logrotate.d/myapp

# Multiple log patterns
/var/log/myapp/*.log
/var/log/myapp/*.err
/var/log/myapp/*.out {
    weekly
    rotate 52            # Keep 1 year
    compress
    delaycompress
    missingok
    notifempty
    size 50M            # Also rotate if > 50MB
    create 644 myapp myapp
    sharedscripts
    postrotate
        # Notify app to reopen logs
        /usr/bin/killall -USR1 myapp 2>/dev/null || true
    endscript
}

System Logs with Mail

# Send old logs via email before deletion
/var/log/critical/*.log {
    weekly
    rotate 4
    compress
    mail [email protected]
    mailfirst           # Mail before rotating
    missingok
    notifempty
    create 600 root root
}

โœ… Step 4: Testing and Troubleshooting

Always test before going live! ๐Ÿงช

Test Configuration

# Dry run - see what would happen
sudo logrotate -d /etc/logrotate.d/myapp

# Force rotation to test
sudo logrotate -f /etc/logrotate.d/myapp

# Verbose mode
sudo logrotate -v /etc/logrotate.d/myapp

# Debug specific config
sudo logrotate -d /etc/logrotate.conf 2>&1 | grep myapp

Check Status

# See last rotation times
cat /var/lib/logrotate/logrotate.status

# Find specific app
grep myapp /var/lib/logrotate/logrotate.status

# Watch logs being rotated
ls -la /var/log/myapp/

# Monitor disk usage
df -h /var/log
du -sh /var/log/*

๐ŸŽฎ Quick Examples

Example 1: Emergency Log Cleanup Script ๐Ÿšจ

#!/bin/bash
# Emergency log cleanup when disk is full

echo "๐Ÿงน Emergency Log Cleanup!"

# Check disk usage
USAGE=$(df /var/log | tail -1 | awk '{print $5}' | sed 's/%//')

if [ $USAGE -gt 80 ]; then
    echo "โš ๏ธ Disk usage critical: ${USAGE}%"
    
    # Force rotate all logs
    echo "Forcing log rotation..."
    logrotate -f /etc/logrotate.conf
    
    # Find and compress uncompressed logs
    find /var/log -name "*.log" -type f -size +100M -exec gzip {} \;
    
    # Delete old compressed logs
    find /var/log -name "*.gz" -mtime +30 -delete
    
    echo "โœ… Cleanup complete!"
    df -h /var/log
else
    echo "โœ… Disk usage OK: ${USAGE}%"
fi

Example 2: Custom Log Monitor ๐Ÿ“Š

#!/bin/bash
# Monitor log growth and alert

LOG_DIR="/var/log"
THRESHOLD=1000  # MB

# Create monitoring function
monitor_logs() {
    echo "๐Ÿ“Š Log Size Report - $(date)"
    echo "================================"
    
    # Find large logs
    large_logs=$(find $LOG_DIR -type f -size +${THRESHOLD}M 2>/dev/null)
    
    if [ ! -z "$large_logs" ]; then
        echo "โš ๏ธ Large logs found:"
        for log in $large_logs; do
            size=$(ls -lh "$log" | awk '{print $5}')
            echo "  $log: $size"
            
            # Auto-rotate if it's configured
            config=$(grep -l "$log" /etc/logrotate.d/* 2>/dev/null)
            if [ ! -z "$config" ]; then
                echo "    โ†’ Auto-rotating using $config"
                logrotate -f "$config"
            else
                echo "    โ†’ No logrotate config found!"
            fi
        done
    else
        echo "โœ… No logs exceed ${THRESHOLD}MB"
    fi
    
    echo ""
    echo "Top 10 largest logs:"
    find $LOG_DIR -type f -name "*.log" -exec ls -s {} \; | \
        sort -rn | head -10 | \
        while read size file; do
            echo "  $(ls -lh "$file" | awk '{print $5}') - $file"
        done
}

monitor_logs

Example 3: Automated Config Generator ๐Ÿค–

#!/bin/bash
# Generate logrotate configs automatically

generate_logrotate_config() {
    local app_name=$1
    local log_path=$2
    local keep_days=${3:-30}
    local user=${4:-root}
    local group=${5:-root}
    
    config_file="/etc/logrotate.d/${app_name}"
    
    echo "๐Ÿ“ Generating logrotate config for $app_name..."
    
    cat > "$config_file" << EOF
# Auto-generated logrotate config for $app_name
# Generated on $(date)

$log_path {
    daily
    rotate $keep_days
    compress
    delaycompress
    missingok
    notifempty
    create 644 $user $group
    size 100M
    
    postrotate
        # Reload or restart service if exists
        if systemctl is-active ${app_name}.service > /dev/null 2>&1; then
            systemctl reload ${app_name}.service
        fi
    endscript
}
EOF
    
    echo "โœ… Config created: $config_file"
    echo "Testing configuration..."
    
    if logrotate -d "$config_file" 2>&1 | grep -q "error"; then
        echo "โŒ Configuration has errors!"
    else
        echo "โœ… Configuration is valid!"
    fi
}

# Usage examples
generate_logrotate_config "myapp" "/var/log/myapp/*.log" 30 "myapp" "myapp"
generate_logrotate_config "backup" "/var/log/backup/*.log" 7

๐Ÿšจ Fix Common Problems

Problem 1: Logs Not Rotating โŒ

Logs growing but not rotating?

# Check if logrotate is running
systemctl status crond

# Look for errors
journalctl -u crond | grep logrotate

# Check config syntax
logrotate -d /etc/logrotate.conf 2>&1 | grep error

# Force rotation
logrotate -f /etc/logrotate.d/yourapp

# Check permissions
ls -la /var/lib/logrotate/

Problem 2: Permission Denied โŒ

Canโ€™t create or rotate logs?

# Check log directory permissions
ls -ld /var/log/yourapp/

# Fix ownership
sudo chown -R appuser:appgroup /var/log/yourapp/

# SELinux issues?
getenforce
sudo semanage fcontext -a -t var_log_t "/var/log/yourapp(/.*)?"
sudo restorecon -Rv /var/log/yourapp

Problem 3: Disk Still Full โŒ

Rotated but disk still full?

# Find old rotated logs
find /var/log -name "*.gz" -mtime +30

# Clean them up
find /var/log -name "*.gz" -mtime +30 -delete

# Check for uncompressed old logs
find /var/log -name "*.1" -o -name "*.2"

# Compress them manually
find /var/log -name "*.[0-9]" -exec gzip {} \;

Problem 4: Application Canโ€™t Write After Rotation โŒ

App stops logging after rotation?

# Add copytruncate option
/var/log/app/*.log {
    daily
    rotate 7
    compress
    copytruncate  # Copy log and truncate instead of moving
    missingok
    notifempty
}

# Or signal app to reopen logs
postrotate
    kill -USR1 $(cat /var/run/app.pid)
endscript

๐Ÿ“‹ Simple Commands Summary

TaskCommand
๐Ÿ“ Test configlogrotate -d /etc/logrotate.d/app
๐Ÿ”„ Force rotationlogrotate -f /etc/logrotate.d/app
๐Ÿ‘€ Check statuscat /var/lib/logrotate/logrotate.status
๐Ÿ“Š List configsls /etc/logrotate.d/
๐Ÿ” Debug modelogrotate -dv /etc/logrotate.conf
โœ๏ธ Edit main configsudo nano /etc/logrotate.conf
๐Ÿ—œ๏ธ Compress logsgzip /var/log/*.log
๐Ÿ“ˆ Check sizesdu -sh /var/log/*

๐Ÿ’ก Tips for Success

  1. Test First ๐Ÿงช - Always use -d flag before real rotation
  2. Start Simple ๐ŸŽฏ - Basic config, then add features
  3. Monitor Disk ๐Ÿ“Š - Set up disk usage alerts
  4. Compress Old ๐Ÿ—œ๏ธ - Save tons of space with compression
  5. Document ๐Ÿ“ - Comment your configs
  6. Regular Cleanup ๐Ÿงน - Donโ€™t keep logs forever

And hereโ€™s a pro tip: Set up different retention for different logs. Keep security logs longer than debug logs. I learned this after accidentally deleting important audit logs. Oops! ๐Ÿ˜…

๐Ÿ† What You Learned

Youโ€™re now a log rotation expert! You can:

  • โœ… Configure logrotate for any application
  • โœ… Set up size and time-based rotation
  • โœ… Compress and archive old logs
  • โœ… Run scripts during rotation
  • โœ… Test and debug configurations
  • โœ… Prevent disk full disasters
  • โœ… Automate log management

๐ŸŽฏ Why This Matters

Proper log management means:

  • ๐Ÿ’พ Never run out of disk space
  • ๐Ÿ” Find logs when you need them
  • ๐Ÿ“Š Keep compliance requirements
  • ๐Ÿš€ Better system performance
  • ๐Ÿ’ฐ Save on storage costs
  • ๐Ÿ˜ด Sleep peacefully at night

Last week, a client called me in panic - their e-commerce site was down. The culprit? A 75GB debug log! Set up logrotate, problem solved forever. They were so relieved they gave me a bonus. And now you can prevent these disasters too! ๐Ÿ’ช

Remember: Logs are important for debugging, but they shouldnโ€™t crash your system. Set up logrotate once, and itโ€™ll quietly keep things clean forever. Itโ€™s like having a janitor for your logs! ๐Ÿงน

Happy rotating! May your logs stay manageable and your disks never fill up! ๐Ÿ“œโœจ