Automate Daily Log Rotation with Logrotate & Cron

Close-up of a well-organized directory structure on a computer screen.

Managing logs effectively is critical for system stability and security. Uncontrolled logs can quickly fill your storage and impact performance. This guide will show you how to automate daily log rotation using logrotate and cron in a reliable, industry-standard way.

Why Logrotate and Cron?

  • Logrotate: Mature, widely supported tool designed specifically to manage log files, ensuring logs stay manageable and disk usage remains predictable
  • Cron: Simple, proven scheduling utility in Linux, making it ideal for automating periodic tasks.

Assumptions

⚙️ Before You Begin

This tutorial is designed for users with basic Linux command-line knowledge. You’ll need sudo access and a Linux environment (Ubuntu or CentOS recommended).

  • You have administrative access (sudo privileges) to your Linux system.
  • You are using a Debian/Ubuntu or RHEL/CentOS Linux distribution.
  • Your logs are stored under /var/log/moderntechops/.
  • Your system has cron installed and properly configured.

Initial Setup Step

Before configuring log rotation, ensure that the target log files and directories exist and are writable. Execute the following commands to set up an example log file for rotation:

sudo mkdir -p /var/log/moderntechops
sudo touch /var/log/moderntechops/example.log

You can even add some text to simulate real logs:

echo "Test log entry: $(date)" | sudo tee -a /var/log/moderntechops/example.log

Step-by-Step Setup

Step 1: Ensure Logrotate is Installed

On most modern Linux systems, logrotate is installed by default. Verify this by running:

logrotate --version

If not installed, you can add it easily:

  • Debian/Ubuntu:
sudo apt update
sudo apt install logrotate
  • RHEL/CentOS:
sudo yum install logrotate
Terminal output from running the command logrotate --version.

Step 2: Configure Custom Logrotate File

Create a custom configuration file at /etc/logrotate.d/moderntechops:

sudo nano /etc/logrotate.d/moderntechops

Add the following content, adjusting paths and log files accordingly:

/var/log/moderntechops/*.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    create 0640 root adm
    sharedscripts
    postrotate
        systemctl reload rsyslog > /dev/null 2>&1 || true
    endscript
}

Explanation of Configuration:

  • daily: Rotate logs daily.
  • rotate 7: Retain logs for 7 days.
  • compress: Compress older logs to save space.
  • delaycompress: Delay compression until the next rotation.
  • missingok: Avoid errors if logs are missing.
  • notifempty: Don’t rotate empty files.
  • create: Creates new logs with specified permissions and ownership.
  • sharedscripts: Runs scripts only once per rotation cycle.
  • postrotate: Reloads rsyslog after rotation (modify for your logging service).
Vim Text editor showing the logrotate config file clearly formatted.

Step 3: Test Logrotate Configuration

Validate the new configuration with debug mode to catch errors without performing rotation:

sudo logrotate --debug /etc/logrotate.conf

Ensure no errors or warnings appear.

Terminal output of running the command logrotate --debug.
Remaining Terminal output of running the command logrotate --debug.

Step 4: Automate with Cron

Edit your crontab to schedule daily rotation at 6:45 PM:

sudo crontab -e

Add the following line:

45 18 * * * /usr/sbin/logrotate /etc/logrotate.conf

Explanation of the Updated Cron Syntax:

 ┌─────────────── Minute (30)
 │ ┌───────────── Hour (18 = 6 PM in 24-hour format)
 │ │ ┌─────────── Day of Month (* = every day)
 │ │ │ ┌───────── Month (* = every month)
 │ │ │ │ ┌─────── Day of Week (* = every day of the week)
 │ │ │ │ │
30 18 * * * /usr/sbin/logrotate /etc/logrotate.conf
  • 45 = minute (30 minutes past the hour)
  • 18 = 6 PM in 24-hour format (6 PM = 18:00)

Save and exit.

Step 5: Verify Automation

After the cron job runs, confirm logs rotate correctly:

ls -lh /var/log/moderntechops/

You should see compressed .gz archives from past rotations and today’s active log.

Why Choose This Method?

While other tools and custom scripts are available (e.g., custom Bash scripts, systemd timers, or cloud-provider log management solutions), logrotate combined with cron offers the following benefits:

  • Simplicity: Minimal configuration and easy maintenance.
  • Stability: Mature tools with extensive documentation and community support.
  • Efficiency: Lightweight operations that won’t noticeably affect server performance.
  • Compatibility: Widely available on most Linux distributions by default.

Alternatives Considered

  • Systemd timers: Modern alternative to cron. Good for systemd-heavy environments but less universal and familiar than cron.
  • Custom Bash scripts: Offer total flexibility but require manual maintenance and potential debugging overhead.

Given the above, logrotate with cron provides a balanced, reliable solution widely adopted across industries.

Additional References

Conclusion

Automating daily log rotation ensures your server remains healthy and performant. By following these steps, you establish a robust, maintainable logging strategy using proven, reliable tools.

Last updated: June 9, 2025

Leave a Comment

Your email address will not be published. Required fields are marked *