Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Where the Configuration Lives

  • /etc/logrotate.conf: the global defaults, such as how often to rotate and how many copies to keep. It includes the directory below.
  • /etc/logrotate.d/: one file per package or application. Nginx, Apache, and most databases install their own file here.

Settings in a block override the global defaults for the files that block names. For your own application, create a new file in /etc/logrotate.d/ instead of editing the main file.

A Sample Block

Save this as /etc/logrotate.d/myapp:

/var/log/myapp/*.log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 0640 deploy deploy
    sharedscripts
    postrotate
        systemctl reload myapp.service > /dev/null 2>&1 || true
    endscript
}

What Each Line Does

DirectiveMeaning
dailyRotate once a day; also weekly, monthly
rotate 14Keep 14 old files
compressCompress old files with gzip
delaycompressLeave the newest old file uncompressed
missingokNo error if the log is missing
notifemptySkip empty logs
createNew empty log with this mode and owner
sharedscriptsRun the script once for all files

The postrotate section runs after rotation. Most programs keep writing to the file they opened, which after a rename is the old file. A reload tells the program to open the new one. delaycompress exists for the same reason: a program that has not reopened its log yet can still write to the newest rotated file.

Instead of a daily schedule, size 100M rotates when a file passes that size, and maxage 30 deletes rotated files older than 30 days.

copytruncate for Programs That Cannot Reopen

Some applications cannot be told to reopen their log. For them, copytruncate copies the log to the rotated name and then empties the original in place, so the program keeps writing to the same file. The price is a short window between the copy and the truncation in which new lines can be lost. Use it only when a reload signal is not possible, and do not combine it with create.

Test Before You Rely on It

sudo logrotate -d /etc/logrotate.d/myapp

-d runs in debug mode: logrotate prints what it would do and changes nothing. Read the output for errors and for the files it matched. To run a real rotation now, for example to see that the reload works:

sudo logrotate -v -f /etc/logrotate.d/myapp

-f forces a rotation even if one is not due, and -v shows each step. Then check that the application writes to the new file with tail -f.

How It Is Scheduled

logrotate is not a daemon; something has to start it. On current Ubuntu, Debian, AlmaLinux, and Rocky Linux releases this is a systemd timer that runs once a day:

systemctl list-timers logrotate.timer

Older systems start it from a script in /etc/cron.daily. logrotate records the last rotation of each file in a state file under /var/lib/logrotate/; that is how it knows when a file is due.

Common Problems

  • Insecure permissions on the parent directory: logrotate refuses to work in a log directory that everyone, or a group other than root, can write to. Add a su deploy deploy line to the block so it rotates as that user.
  • The disk still fills up: the application writes to the old, deleted file. Fix the postrotate reload; no space left on device shows how to find such files.
  • Nothing rotates: check the timer, and run the debug command above to see whether the path pattern matches.

The Journal Is Separate

logrotate handles text files under /var/log. The systemd journal has its own size limits and is trimmed with journalctl, as described in journalctl. For where each log lives, see Linux log files.

Was this answer helpful?
Loading...