← Full reference

logrotate

Log rotation — rename, compress, delete old copies and let the service start a new file

logrotate doesn't run continuously: it's started on a schedule (cron or a systemd timer, usually once a day). On each run it reads its configs, decides which logs are due to be "rotated", and does it: app.log → app.log.1 → app.log.2.gz…, deletes excess old copies and creates a new empty file for the service.

Syntax

logrotate [options] config

The main config is /etc/logrotate.conf: it holds global settings and the line include /etc/logrotate.d. Rules for individual programs live in /etc/logrotate.d/<name>. The daily run is /etc/cron.daily/logrotate (on systemd systems — logrotate.timer).

Rule format:

/var/log/myapp/*.log {
    daily
    rotate 7
    compress
    missingok
    notifempty
}

First one or more paths (patterns allowed), then directives in curly braces — one per line. Directives outside the braces set defaults for the rules below them.

Command-line options

OptionWhat it does
-ddebug: does nothing, only prints what would be done (implies -v)
-vprint verbosely what's happening
-fforce rotation even if it's not due yet
-s fileyour own state file instead of /var/lib/logrotate/status
-l filewrite a log of its work to a file (or syslog)
-m commandwhat to send mail with (for mail)

When to rotate

DirectiveMeaning
daily / weekly / monthly / yearlyby time since the last rotation
hourlyonce an hour — only if logrotate itself runs every hour
size 100Mwhen the file is larger than the size (time is ignored); units k, M, G
minsize 1Mby time, but only if the file is at least this size
maxsize 500Mby time or earlier if the file exceeds the size
notifempty / ifemptydon't touch / do touch an empty file
missingoka missing file isn't an error

How many to keep and where

DirectiveMeaning
rotate 7keep 7 old copies; rotate 0 — delete old ones right away
maxage 30delete copies older than 30 days
olddir directoryput copies in another directory (on the same filesystem)
dateexta date suffix (app.log-20250115) instead of a number
dateformat -%Y%m%d-%sdate format for dateext
extension .logkeep the extension at the end: app-20250115.log
start 0the number to start from (default 1)

Compression

DirectiveMeaning
compress / nocompresscompress old copies (gzip by default)
delaycompresscompress starting next time: .1 stays uncompressed, the service may still be writing to it
compresscmd, compressext, compressoptionsa different compressor: compresscmd /usr/bin/xz, compressext .xz

How to get a new file

DirectiveMeaning
create 0640 user groupafter renaming, create a new file with these permissions and owner
nocreatedon't create a new file
copytruncatecopy the log and truncate the original — for programs that can't reopen the file
copyonly copy, leave the original alone
su user grouprotate as this user — needed if the log directory isn't owned by root

Scripts

DirectiveWhen it runs
prerotate … endscriptbefore rotation
postrotate … endscriptafter rotation — usually where you signal the service to reopen its log
firstaction / lastaction … endscriptonce before / after processing all files of the rule
sharedscriptspre/postrotate run once for the whole rule rather than for each file matching the pattern

Scripts run via /bin/sh.

Examples

# what would be done — without changes
logrotate -d /etc/logrotate.d/nginx

# test your config, forced and verbose
logrotate -f -v /etc/logrotate.d/myapp

# run everything the way cron does
logrotate /etc/logrotate.conf

# your own config with your own state file — without touching the system one
logrotate -s /tmp/lr.state /path/to/test.conf

# when each log was last rotated
cat /var/lib/logrotate/status

A rule for a program that reopens its logs on HUP:

/var/log/myapp/*.log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 0640 root adm
    sharedscripts
    postrotate
        kill -HUP "$(cat /run/myapp.pid)" 2>/dev/null || true
    endscript
}

Pitfalls

  • The first run rotates nothing. If the log isn't in the state file, logrotate records "rotated now" and waits for the next period. Use -f for testing.
  • -d does nothing — it's only a dry run. To see the result you need -f (and -v).
  • Without postrotate or copytruncate the service writes to the old file. Renaming doesn't change the open descriptor: the process keeps writing to app.log.1, and the new app.log stays empty.
  • copytruncate can lose lines written between the copy and the truncation. If the service can reopen its logs, create + postrotate is better.
  • notifempty wins even over -f: an empty log isn't rotated.
  • A * pattern catches already rotated copies. /var/log/app/* turns app.log.1 into app.log.1.1. Write *.log.
  • A config writable by the group or others is ignored ("writable by group or others"). Permissions 644, owner root.
  • A log directory writable by non-root is skipped too: you need the su directive.
  • A standalone config doesn't inherit /etc/logrotate.conf. When you run logrotate my.conf, the global create, weekly and the rest don't apply — put everything you need in the rule.
  • The size is written without B: size 10M, not 10MB — otherwise you get "unknown unit".
  • In a container it won't run by itself: the daily call comes from cron, and cron needs to be started (service cron start).

See also

cron · kill · gzip · df-du