Console9

Logrotate directives reference

Complete reference table for all Logrotate configuration directives including rotation, compression, retention, hooks, and file creation 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.

DirectiveDescriptionDefaultExample
dailyRotates the log file every day. Logrotate checks the timestamp from its state file to determine if a full day has passed.offdaily
weeklyRotates the log file once per week. Logrotate triggers rotation on the first day of the week when invoked.offweekly
monthlyRotates the log file once per month. Logrotate triggers rotation on the first day of the month when invoked.offmonthly
yearlyRotates the log file once per year. Logrotate triggers rotation on the first day of the year when invoked.offyearly
hourlyRotates the log file every hour. Requires a custom cron job that runs Logrotate more frequently than the default daily schedule.offhourly

Logrotate Rotation Count and Size Directives

Logrotate controls how many rotated copies to keep and when to trigger size-based rotation.

DirectiveDescriptionDefaultExample
rotate NKeeps N rotated copies of the log file before Logrotate deletes the oldest. A value of 0 deletes old logs immediately after rotation.0rotate 7
size NRotates the log file when it exceeds N bytes. Accepts k (kilobytes), M (megabytes), and G (gigabytes) suffixes. Overrides time-based rotation.nonesize 100M
minsize NRotates the log file only if it exceeds N bytes AND the time interval has passed. Combines size and time conditions.noneminsize 50M
maxsize NRotates the log file when it exceeds N bytes regardless of the time interval. Forces rotation even if the schedule has not elapsed.nonemaxsize 500M

Logrotate Compression Directives

Logrotate compresses rotated files to reduce disk usage. These directives control compression behavior.

DirectiveDescriptionDefaultExample
compressCompresses rotated log files with gzip by default. Reduces disk space used by old log copies.offcompress
nocompressDisables compression of rotated log files. Overrides a global compress setting.onnocompress
delaycompressPostpones compression until the next rotation cycle. Prevents conflicts with applications that may still write to the previous log file. Requires compress.offdelaycompress
nodelaycompressDisables delayed compression. Logrotate compresses the rotated file immediately.onnodelaycompress
compresscmdSpecifies the compression program Logrotate uses instead of the default gzip.gzipcompresscmd bzip2
compressoptionsPasses command-line options to the compression program. The default for gzip is -9 (maximum compression).-9compressoptions -6
compressextSpecifies the file extension for compressed files. Logrotate appends this suffix after compression..gzcompressext .bz2

Logrotate File Retention and Age Directives

Logrotate removes old rotated files based on age or count thresholds.

DirectiveDescriptionDefaultExample
maxage NRemoves rotated log files older than N days. Logrotate deletes files based on their last modification timestamp.nonemaxage 30
minage NSkips rotation unless the log file is at least N days old. Prevents rotation of recently created files.noneminage 1

Logrotate File Creation and Naming Directives

Logrotate controls how it handles the original log file after rotation and how it names rotated copies.

DirectiveDescriptionDefaultExample
create mode owner groupCreates 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
nocreateDisables creation of a new log file after rotation. Use when the application creates its own log file on startup.offnocreate
copytruncateCopies 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.offcopytruncate
copyMakes a copy of the log file without truncating the original. The original file remains in place with its full content.offcopy
dateextAppends a date stamp (e.g., -20260308) to rotated filenames instead of a numeric suffix (e.g., .1, .2).offdateext
nodateextDisables date-based extensions. Logrotate uses numeric suffixes for rotated files.onnodateext
dateformat format_stringSets the date format for rotated filenames when dateext is active. Uses strftime format specifiers.-%Y%m%ddateformat -%Y%m%d
extension extPreserves the specified file extension on rotated files. Logrotate places the rotation suffix before the extension.noneextension .log
olddir directoryMoves rotated log files to the specified directory instead of keeping them alongside the original. The directory must exist.same directoryolddir /var/log/old
noolddirKeeps rotated files in the same directory as the original log file. Overrides a global olddir setting.onnoolddir

Logrotate Script Hook Directives

Logrotate executes custom scripts at specific points during the rotation process. All script blocks end with the endscript keyword.

DirectiveDescriptionDefaultExample
prerotate/ endscriptExecutes the enclosed script before Logrotate rotates the log file. Runs once per matched file unless sharedscripts is active.noneSee below
postrotate/ endscriptExecutes the enclosed script after Logrotate rotates the log file. Commonly used to signal the application to reopen its log file.noneSee below
firstaction/ endscriptExecutes the enclosed script once before any log files in the block are rotated. Runs before prerotate.noneSee below
lastaction/ endscriptExecutes the enclosed script once after all log files in the block are rotated. Runs after postrotate.noneSee below
sharedscriptsRuns prerotate and postrotate scripts only once for the entire set of matched files instead of once per file.offsharedscripts
nosharedscriptsRuns prerotate and postrotate scripts once per matched file. Overrides a global sharedscripts setting.onnosharedscripts

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.

DirectiveDescriptionDefaultExample
missingokSuppresses error messages when a configured log file does not exist. Logrotate proceeds to the next file.offmissingok
nomissingokReports an error when a configured log file is missing. Overrides a global missingok setting.onnomissingok
ifemptyAllows Logrotate to rotate the log file even when it is empty.onifempty
notifemptySkips rotation when the log file contains zero bytes. Prevents accumulation of empty rotated files.offnotifempty

Logrotate Mail Directives

Logrotate can mail log files to a specified address when they are rotated or about to be removed.

DirectiveDescriptionDefaultExample
mail addressSends rotated log files to the specified email address. Requires a working mail transfer agent on the system.nonemail admin@example.com
mailfirstMails the most recently rotated log file instead of the file about to be removed.offmailfirst
maillastMails the log file that is about to be removed (the oldest rotated copy). This is the default behavior when mail is set.onmaillast
nomailDisables mailing of rotated log files. Overrides a global mail setting.onnomail

Logrotate Security Directives

Logrotate provides directives for secure file deletion and privilege management.

DirectiveDescriptionDefaultExample
su user groupRuns rotation as the specified user and group instead of root. Required when log directories are not owned by root.rootsu www-data adm
shredOverwrites log file content with random data before deletion using the shred utility. Prevents recovery of sensitive log data.offshred
noshredDisables secure overwriting before deletion. Logrotate deletes files normally.onnoshred
shredcycles NSets the number of overwrite passes before Logrotate deletes the file. Higher values increase security but reduce performance.3shredcycles 5

Logrotate Include and Filter Directives

Logrotate supports including additional configuration files and filtering which files to process.

DirectiveDescriptionDefaultExample
include pathReads configuration from the specified file or all files in the specified directory. The main /etc/logrotate.conf typically includes /etc/logrotate.d/.noneinclude /etc/logrotate.d
tabooext extExcludes 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.

OptionDescriptionExample
-f/ --forceForces immediate rotation of all configured log files regardless of schedule or size conditions.logrotate -f /etc/logrotate.conf
-d/ --debugRuns Logrotate in debug mode. Simulates rotation and prints actions without modifying any files or updating the state file.logrotate -d /etc/logrotate.conf
-v/ --verboseEnables verbose output. Logrotate prints detailed information about each file it processes.logrotate -v /etc/logrotate.conf
-s file/ --state fileUses 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 fileWrites Logrotate output to the specified log file instead of standard output.logrotate -l /var/log/logrotate.log /etc/logrotate.conf