How to Set Up Log Rotation with logrotate

Without log rotation, log files grow indefinitely, eventually consuming all available disk space. logrotate automates compressing, archiving, and deleting old logs on a schedule.

Why Log Rotation Matters

An application or service logging continuously can generate gigabytes of data over time — without rotation, this eventually fills the disk, potentially causing the application (or the entire server) to fail when storage is exhausted.

logrotate Is Usually Pre-Installed

which logrotate

Most distributions include logrotate by default and already rotate common system logs (syslog, auth.log) automatically — this guide covers adding rotation for your own application's logs.

Step 1 — Create a Configuration File for Your Application

sudo nano /etc/logrotate.d/myapp
/var/log/myapp/*.log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 0640 www-data www-data
}

Understanding Each Directive

DirectiveMeaning
dailyRotate logs once per day
rotate 14Keep 14 rotated copies before deleting the oldest
compressGzip rotated logs to save space
delaycompressDelay compression by one cycle, useful if a process might still be writing to the just-rotated file briefly
missingokDon't error if the log file doesn't exist
notifemptySkip rotation if the log file is empty
createCreate a new empty log file with specified permissions after rotation

Step 2 — Test the Configuration

sudo logrotate -d /etc/logrotate.d/myapp

-d runs in debug/dry-run mode, showing what would happen without actually performing the rotation — verify the output matches your expectations before relying on it.

Step 3 — Force an Actual Rotation to Verify

sudo logrotate -f /etc/logrotate.d/myapp

Rotating Based on Size Instead of (or in Addition to) Time

/var/log/myapp/*.log {
    size 100M
    rotate 5
    compress
}

Running a Command After Rotation (postrotate)

/var/log/myapp/*.log {
    daily
    rotate 14
    compress
    postrotate
        systemctl reload myapp
    endscript
}

Some applications hold a file handle to the log file and need a reload signal after rotation to start writing to the new file — check your specific application's logging behavior.

Verifying logrotate Runs on Schedule

cat /etc/cron.daily/logrotate

logrotate typically runs via a daily cron job already configured system-wide; your custom configuration file is automatically picked up without additional scheduling needed.

Checking Rotation History

cat /var/lib/logrotate/status

Shows when each configured log was last rotated.

Common Errors

Log file grows despite logrotate configuration — verify the application actually reopens/recreates its log file handle after rotation (via postrotate or the application's own log-reopening behavior); otherwise it may keep writing to the old, now-renamed file indefinitely.

Continue Reading

Browse more articles in Linux Server Administration.

  • logrotate, log rotation linux, manage log files, prevent disk full logs
  • 0 Kasutajad peavad seda kasulikuks
Kas see vastus oli kasulik?

Seotud artiklid

How to Update and Upgrade Your Ubuntu or Debian VPS

Keeping your VPS updated is one of the most important and simplest maintenance tasks — it...

How to Enable and Configure Swap on a Linux VPS (Ubuntu & Debian)

Swap is disk space Linux can use as overflow memory when physical RAM is exhausted. It won't make...

How to Manage Services with systemd and systemctl

systemd is the service manager used by Ubuntu, Debian, and most modern Linux distributions to...

How to Read and Analyze Linux Logs with journalctl

On modern Ubuntu and Debian systems, journalctl is the central tool for viewing system and...

How to Schedule Tasks with Cron on a Linux VPS

Cron is the standard Linux job scheduler, used to automate recurring tasks like backups, log...