Back to tools

Logrotate Generator

What is Logrotate?

Logrotate is a Linux utility designed to manage log files on Unix/Linux systems. It automatically compresses, rotates, deletes, and emails log files according to a scheduled configuration.

Why use logrotate?

  • Space saving: Prevents log files from growing indefinitely and filling the disk.
  • Organization: Maintains a history of rotated logs with dates.
  • Compression: Reduces the size of older logs.
  • Automation: Everything works automatically without manual intervention.

Installation

# Debian/Ubuntu

sudo apt-get install logrotate

# RHEL/CentOS/Fedora

sudo yum install logrotate

# Arch Linux

sudo pacman -S logrotate

Basic Configuration

Typical configuration is located in:

  • /etc/logrotate.conf - Global configuration
  • /etc/logrotate.d/ - Per-application configurations

Important Directives

  • daily/weekly/monthly - Rotation frequency
  • rotate N - Keep N old files
  • size Size - Rotate if size exceeds (e.g., 100M)
  • compress - Compress old logs with gzip
  • create mode user group - Create new file after rotation
  • missingok - No error if file doesn't exist
  • notifempty - Don't rotate if file is empty
  • postrotate/endscript - Run commands after rotation

Practical Example

/var/log/nginx/access.log {

daily

rotate 14

compress

delaycompress

missingok

notifempty

create 640 www-data adm

sharedscripts

postrotate

[ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)

endscript

}

Frequently Asked Questions

What is logrotate and what is it used for?

Logrotate is a Linux utility that automatically manages system log files. Its main purpose is to rotate, compress, remove, and email log files based on a scheduled configuration, preventing them from growing indefinitely and filling up the disk. It is essential for maintaining any Linux production server.

How to install logrotate on Linux?

Logrotate comes preinstalled on most Linux distributions. If not installed, you can add it easily: on Debian/Ubuntu with "sudo apt-get install logrotate", on RHEL/CentOS/Fedora with "sudo yum install logrotate", and on Arch Linux with "sudo pacman -S logrotate". The global configuration is at /etc/logrotate.conf, with per-application configs in /etc/logrotate.d/.

What is the difference between daily, weekly, and monthly directives in logrotate?

These directives define the rotation frequency: "daily" rotates logs every day, "weekly" every week, and "monthly" every month. The choice depends on your application's log volume. For high-traffic web servers like Nginx or Apache, "daily" is recommended. For low-traffic services, "weekly" or "monthly" is usually sufficient.

How to compress old logs with logrotate?

To compress old logs, use the "compress" directive in your logrotate configuration. This compresses rotated logs with gzip, significantly reducing disk space usage. If you need the most recent log to remain uncompressed, combine it with "delaycompress", which delays compression by one rotation cycle.

What does the "rotate" directive mean in logrotate?

The "rotate N" directive specifies how many rotated log files to keep before deleting them. For example, "rotate 14" keeps the last 14 rotated files and deletes the oldest when the 15th arrives. With "daily" and "rotate 14", you retain 14 days of history.

How to force log rotation without waiting for the logrotate schedule?

You can force rotation manually by running "sudo logrotate -f /etc/logrotate.conf" or for a specific file "sudo logrotate -f /etc/logrotate.d/nginx". The "-f" (force) option runs rotation immediately regardless of configured frequency.

How to configure logrotate for Nginx or Apache?

For Nginx, create a file in /etc/logrotate.d/nginx with the appropriate configuration, including a postrotate block sending USR1 signal to Nginx. For Apache the process is similar, using "apache2ctl graceful" in postrotate. This tool automatically generates ready-to-use configurations for both servers.