Master Log Rotation with logrotate: Keep Your Servers Clean and Efficient
This guide explains how logrotate automates log rotation, detailing its configuration files, common directives such as rotate, daily, size, compress, postrotate, and sharedscripts, and provides practical examples for managing large log files efficiently on Linux servers.
How logrotate Works
Logrotate is typically run daily by a cron job (e.g.,
/etc/cron.daily/logrotate). When executed, it reads its configuration files to determine which logs to rotate, how often, and how many archived copies to keep.
logrotate.conf
The main configuration file is
/etc/logrotate.conf. It defines default parameters for all rotations and includes comments for guidance. It also contains a line such as:
<code>include /etc/logrotate.d</code>This directory holds most application‑specific configuration files.
logrotate.d
To list the files in this directory, run:
<code>ls /etc/logrotate.d</code>The directory may be empty or contain many files, depending on installed packages. Packages typically drop a configuration file here—for example, a syslog configuration that defines how system logs are rotated.
Note: Ubuntu versions prior to 9.10 (Karmic Koala) did not have a syslog entry; log rotation was performed by the savelog command from /etc/cron.daily/sysklogd .
Application Configuration Example
An example for Apache on a Fedora system:
<code>/var/log/httpd/*log {</code>
<code> missingok</code>
<code> notifempty</code>
<code> sharedscripts</code>
<code> postrotate</code>
<code> /sbin/service httpd reload > /dev/null 2>/dev/null || true</code>
<code> endscript</code>
<code>}</code>When logrotate runs, it checks all non‑empty files ending with
logunder
/var/log/httpd. If none are found, no error is raised. After processing the files, the
postrotate/endscriptblock is executed, restarting Apache only once if multiple logs are rotated.
Common Directives
Use
man logrotatefor the full list. Frequently used commands include:
rotate 4– keep four archived logs before deleting older ones.
daily,
weekly,
monthly,
yearly– set the rotation interval.
size 100M– rotate when the log exceeds 100 MB (size overrides interval if both are set).
compress– gzip the rotated logs.
nocompress– disable compression for a specific block.
delaycompress– postpone compression until the next rotation, useful when a service may still write to the old file after a restart.
postrotate … endscript– run commands after rotation (e.g., restart a service).
sharedscripts– ensure the
postrotatescript runs only once per block, even if multiple logs are rotated.
Sample Block with Multiple Logs
<code>/var/foo/*.log /var/bar/log.txt {</code>
<code> rotate 14</code>
<code> daily</code>
<code> compress</code>
<code> delaycompress</code>
<code> sharedscripts</code>
<code> postrotate</code>
<code> /usr/sbin/apachectl graceful > /dev/null</code>
<code> endscript</code>
<code>}</code>Where to Go Next
This overview covers logrotate’s purpose and its most useful configuration options. You should now be able to inspect existing configurations and adapt them to your needs. For examples on rotating custom virtual‑host logs, refer to sample logrotate configurations and troubleshooting resources.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.