Logrotate directives reference
Complete reference table for all Logrotate configuration directives including rotation, compression, retention, hooks, and file creation options.
- Logrotate Rotation Frequency Directives
- Logrotate Rotation Count and Size Directives
- Logrotate Compression Directives
- Logrotate File Retention and Age Directives
- Logrotate File Creation and Naming Directives
- Logrotate Script Hook Directives
- Logrotate Postrotate Example for Nginx
- Logrotate Error Handling Directives
- Logrotate Mail Directives
- Logrotate Security Directives
- Logrotate Include and Filter Directives
- Logrotate Command-Line Options
Complete reference for all Logrotate configuration directives and command-line options.
Logrotate Rotation Frequency Directives
Logrotate uses time-based directives to control how often it rotates each log file. Place these directives inside a log file block in
/etc/logrotate.d/ or in
/etc/logrotate.conf.
| Directive | Description | Default | Example |
|---|---|---|---|
daily | Rotates the log file every day. Logrotate checks the timestamp from its state file to determine if a full day has passed. | off | daily |
weekly | Rotates the log file once per week. Logrotate triggers rotation on the first day of the week when invoked. | off | weekly |
monthly | Rotates the log file once per month. Logrotate triggers rotation on the first day of the month when invoked. | off | monthly |
yearly | Rotates the log file once per year. Logrotate triggers rotation on the first day of the year when invoked. | off | yearly |
hourly | Rotates the log file every hour. Requires a custom cron job that runs Logrotate more frequently than the default daily schedule. | off | hourly |
Logrotate Rotation Count and Size Directives
Logrotate controls how many rotated copies to keep and when to trigger size-based rotation.
| Directive | Description | Default | Example |
|---|---|---|---|
rotate N | Keeps N rotated copies of the log file before Logrotate deletes the oldest. A value of
0 deletes old logs immediately after rotation. | 0 | rotate 7 |
size N | Rotates the log file when it exceeds N bytes. Accepts
k (kilobytes),
M (megabytes), and
G (gigabytes) suffixes. Overrides time-based rotation. | none | size 100M |
minsize N | Rotates the log file only if it exceeds N bytes AND the time interval has passed. Combines size and time conditions. | none | minsize 50M |
maxsize N | Rotates the log file when it exceeds N bytes regardless of the time interval. Forces rotation even if the schedule has not elapsed. | none | maxsize 500M |
Logrotate Compression Directives
Logrotate compresses rotated files to reduce disk usage. These directives control compression behavior.
| Directive | Description | Default | Example |
|---|---|---|---|
compress | Compresses rotated log files with gzip by default. Reduces disk space used by old log copies. | off | compress |
nocompress | Disables compression of rotated log files. Overrides a global
compress setting. | on | nocompress |
delaycompress | Postpones compression until the next rotation cycle. Prevents conflicts with applications that may still write to the previous log file. Requires
compress. | off | delaycompress |
nodelaycompress | Disables delayed compression. Logrotate compresses the rotated file immediately. | on | nodelaycompress |
compresscmd | Specifies the compression program Logrotate uses instead of the default gzip. | gzip | compresscmd bzip2 |
compressoptions | Passes command-line options to the compression program. The default for gzip is
-9 (maximum compression). | -9 | compressoptions -6 |
compressext | Specifies the file extension for compressed files. Logrotate appends this suffix after compression. | .gz | compressext .bz2 |
Logrotate File Retention and Age Directives
Logrotate removes old rotated files based on age or count thresholds.
| Directive | Description | Default | Example |
|---|---|---|---|
maxage N | Removes rotated log files older than N days. Logrotate deletes files based on their last modification timestamp. | none | maxage 30 |
minage N | Skips rotation unless the log file is at least N days old. Prevents rotation of recently created files. | none | minage 1 |
Logrotate File Creation and Naming Directives
Logrotate controls how it handles the original log file after rotation and how it names rotated copies.
| Directive | Description | Default | Example |
|---|---|---|---|
create mode owner group | Creates a new empty log file with the specified permissions, owner, and group after rotation. Ensures the application can write to the new file. | create (inherits original) | create 0640 www-data adm |
nocreate | Disables creation of a new log file after rotation. Use when the application creates its own log file on startup. | off | nocreate |
copytruncate | Copies the log file content to the rotated file and truncates the original to zero bytes. Avoids the need to signal the application to reopen its log file. Small risk of data loss between the copy and truncate operations. | off | copytruncate |
copy | Makes a copy of the log file without truncating the original. The original file remains in place with its full content. | off | copy |
dateext | Appends a date stamp (e.g.,
-20260308) to rotated filenames instead of a numeric suffix (e.g.,
.1,
.2). | off | dateext |
nodateext | Disables date-based extensions. Logrotate uses numeric suffixes for rotated files. | on | nodateext |
dateformat format_string | Sets the date format for rotated filenames when
dateext is active. Uses strftime format specifiers. | -%Y%m%d | dateformat -%Y%m%d |
extension ext | Preserves the specified file extension on rotated files. Logrotate places the rotation suffix before the extension. | none | extension .log |
olddir directory | Moves rotated log files to the specified directory instead of keeping them alongside the original. The directory must exist. | same directory | olddir /var/log/old |
noolddir | Keeps rotated files in the same directory as the original log file. Overrides a global
olddir setting. | on | noolddir |
Logrotate Script Hook Directives
Logrotate executes custom scripts at specific points during the rotation process. All script blocks end with the
endscript keyword.
| Directive | Description | Default | Example |
|---|---|---|---|
prerotate/
endscript | Executes the enclosed script before Logrotate rotates the log file. Runs once per matched file unless
sharedscripts is active. | none | See below |
postrotate/
endscript | Executes the enclosed script after Logrotate rotates the log file. Commonly used to signal the application to reopen its log file. | none | See below |
firstaction/
endscript | Executes the enclosed script once before any log files in the block are rotated. Runs before
prerotate. | none | See below |
lastaction/
endscript | Executes the enclosed script once after all log files in the block are rotated. Runs after
postrotate. | none | See below |
sharedscripts | Runs
prerotate and
postrotate scripts only once for the entire set of matched files instead of once per file. | off | sharedscripts |
nosharedscripts | Runs
prerotate and
postrotate scripts once per matched file. Overrides a global
sharedscripts setting. | on | nosharedscripts |
Logrotate Postrotate Example for Nginx
Logrotate commonly uses
postrotate to send a signal that tells Nginx to reopen its log files:
/var/log/nginx/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)
endscript
}Logrotate Error Handling Directives
Logrotate provides directives to control how it handles missing or empty log files.
| Directive | Description | Default | Example |
|---|---|---|---|
missingok | Suppresses error messages when a configured log file does not exist. Logrotate proceeds to the next file. | off | missingok |
nomissingok | Reports an error when a configured log file is missing. Overrides a global
missingok setting. | on | nomissingok |
ifempty | Allows Logrotate to rotate the log file even when it is empty. | on | ifempty |
notifempty | Skips rotation when the log file contains zero bytes. Prevents accumulation of empty rotated files. | off | notifempty |
Logrotate Mail Directives
Logrotate can mail log files to a specified address when they are rotated or about to be removed.
| Directive | Description | Default | Example |
|---|---|---|---|
mail address | Sends rotated log files to the specified email address. Requires a working mail transfer agent on the system. | none | mail admin@example.com |
mailfirst | Mails the most recently rotated log file instead of the file about to be removed. | off | mailfirst |
maillast | Mails the log file that is about to be removed (the oldest rotated copy). This is the default behavior when
mail is set. | on | maillast |
nomail | Disables mailing of rotated log files. Overrides a global
mail setting. | on | nomail |
Logrotate Security Directives
Logrotate provides directives for secure file deletion and privilege management.
| Directive | Description | Default | Example |
|---|---|---|---|
su user group | Runs rotation as the specified user and group instead of root. Required when log directories are not owned by root. | root | su www-data adm |
shred | Overwrites log file content with random data before deletion using the
shred utility. Prevents recovery of sensitive log data. | off | shred |
noshred | Disables secure overwriting before deletion. Logrotate deletes files normally. | on | noshred |
shredcycles N | Sets the number of overwrite passes before Logrotate deletes the file. Higher values increase security but reduce performance. | 3 | shredcycles 5 |
Logrotate Include and Filter Directives
Logrotate supports including additional configuration files and filtering which files to process.
| Directive | Description | Default | Example |
|---|---|---|---|
include path | Reads configuration from the specified file or all files in the specified directory. The main
/etc/logrotate.conf typically includes
/etc/logrotate.d/. | none | include /etc/logrotate.d |
tabooext ext | Excludes files with the specified extension from processing when Logrotate reads a directory with
include. Prevents backup files from being treated as configuration. | .rpmsave,
.rpmorig, etc. | tabooext .bak |
Logrotate Command-Line Options
Logrotate accepts command-line flags to control its runtime behavior.
| Option | Description | Example |
|---|---|---|
-f/
--force | Forces immediate rotation of all configured log files regardless of schedule or size conditions. | logrotate -f /etc/logrotate.conf |
-d/
--debug | Runs Logrotate in debug mode. Simulates rotation and prints actions without modifying any files or updating the state file. | logrotate -d /etc/logrotate.conf |
-v/
--verbose | Enables verbose output. Logrotate prints detailed information about each file it processes. | logrotate -v /etc/logrotate.conf |
-s file/
--state file | Uses an alternate state file instead of the default
/var/lib/logrotate/status. Necessary when running multiple Logrotate instances with separate schedules. | logrotate -s /var/lib/logrotate/hourly.status /etc/logrotate-hourly.conf |
-l file/
--log file | Writes Logrotate output to the specified log file instead of standard output. | logrotate -l /var/log/logrotate.log /etc/logrotate.conf |