TExcelTAD
Home › Linux Server Administration › Log Files & logrotate

Log Files & logrotate

The journal rotates itself, but plenty of logs are still plain files: Nginx access and error logs, /var/log/auth.log written by rsyslog, application logs in /var/log/<app>/. A file that only ever grows fills the disk eventually; the classic 3 a.m. outage is "/var is 100% full because of access.log". logrotate prevents that. A systemd timer (logrotate.timer, daily) runs it with /etc/logrotate.conf, which includes every file in /etc/logrotate.d/.

A config, line by line

text
/var/log/inventory/*.log {
    daily               # or weekly, monthly, or size-based (below)
    rotate 14           # keep 14 old files, delete older ones
    compress            # gzip rotated files...
    delaycompress       # ...but not the newest one (a process may still write to it)
    missingok           # no error if no file matches
    notifempty          # don't rotate an empty file
    create 0640 inventory adm   # make a new empty file with this mode and owner
    sharedscripts       # run postrotate once for all matching files, not once per file
    postrotate
        systemctl kill -s HUP inventory.service
    endscript
}

The important design question is how the app learns about the new file. A process writes to an open file descriptor, not to a name: after logrotate renames app.log to app.log.1, the app happily keeps writing into app.log.1. Three ways out:

Strategy How Trade-off
create + signal rename, create a new file, postrotate tells the app to reopen (Nginx: USR1, many daemons: HUP) clean; the app must support reopening
copytruncate copy the file, then truncate the original in place works with any app; lines written between copy and truncate are lost
let the app rotate the app (or its logging library) rotates by itself then logrotate must not touch those files

Seeing what logrotate would do

logrotate -d is a dry run that prints its decisions. With -s the state file (when each log was last rotated) lives in the working directory instead of /var/lib/logrotate/status, and -f forces a rotation regardless of the schedule:

bash · try it yourself
mkdir -p logs && seq 1 1000 > logs/app.log && : > logs/empty.log
cat > rotate.conf <<'EOF'
/home/learner/logs/*.log {
    daily
    rotate 3
    compress
    delaycompress
    missingok
    notifempty
    create 0640
}
EOF
logrotate -d -f -s state rotate.conf 2>&1 | grep -v -e '^warning' -e '^
  

 -e 'Now:' -e 'Last rotated' -e 'hash table' -e dateext -e 'glob pattern'
reading config file rotate.conf
Reading state from file: state
state file state does not exist
Handling 1 logs
rotating pattern: /home/learner/logs/*.log  forced from command line (3 rotations)
empty log files are not rotated, old logs are removed
considering log /home/learner/logs/app.log
Creating new state
  log needs rotating
considering log /home/learner/logs/empty.log
Creating new state
  log does not need rotating (log is empty)
rotating log /home/learner/logs/app.log, log->rotateCount is 3
previous log /home/learner/logs/app.log.1 does not exist
renaming /home/learner/logs/app.log.3.gz to /home/learner/logs/app.log.4.gz (rotatecount 3, logstart 1, i 3),
renaming /home/learner/logs/app.log.2.gz to /home/learner/logs/app.log.3.gz (rotatecount 3, logstart 1, i 2),
renaming /home/learner/logs/app.log.1.gz to /home/learner/logs/app.log.2.gz (rotatecount 3, logstart 1, i 1),
log /home/learner/logs/app.log.4.gz doesn't exist -- won't try to dispose of it
renaming /home/learner/logs/app.log to /home/learner/logs/app.log.1
creating new /home/learner/logs/app.log mode = 0640 uid = 1000 gid = 1000

empty.log is skipped (notifempty), and the plan for app.log reads like a recipe: shift the older generations up by one (.2.gz → .3.gz, none exist yet), rename app.log to app.log.1, create a new app.log with mode 0640, and compress nothing yet (delaycompress). (Two lines about date suffixes were filtered out because they contain today's date.)

Watching a rotation history build up

Now for real, four forced rotations with a new line of log between each. rotate 3 keeps three old files, and delaycompress leaves the newest one uncompressed:

bash · try it yourself
mkdir -p logs
cat > rotate.conf <<'EOF'
/home/learner/logs/app.log {
    rotate 3
    compress
    delaycompress
    create 0640
}
EOF
for day in 1 2 3 4; do
  echo "events of day $day" >> logs/app.log
  logrotate -f -s state rotate.conf
  echo "after rotation $day: $(cd logs && ls | sort | tr '\n' ' ')"
done
zcat logs/app.log.2.gz
cat logs/app.log.1
stat -c '%a %n' logs/app.log
after rotation 1: app.log app.log.1
after rotation 2: app.log app.log.1 app.log.2.gz
after rotation 3: app.log app.log.1 app.log.2.gz app.log.3.gz
after rotation 4: app.log app.log.1 app.log.2.gz app.log.3.gz
events of day 3
events of day 4
640 logs/app.log

The oldest generation (events of day 1) was deleted at the fourth rotation, and the new, empty app.log got the mode from create 0640.

Size-based rotation and the timer

daily only means "at most once a day, when logrotate runs". For logs that can explode (a debug flag left on, a bot hammering the site), add a size limit:

text
/var/log/nginx/*.log {
    daily
    maxsize 500M        # rotate early when bigger than 500 MB...
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        invoke-rc.d nginx rotate >/dev/null 2>&1
    endscript
}

maxsize only helps if logrotate runs often enough to notice; the stock timer runs daily, so for this you would add a drop-in to logrotate.timer with OnCalendar=hourly (systemd Timers). Check the result with systemctl list-timers logrotate.timer and test a config change with sudo logrotate -d /etc/logrotate.d/nginx before the night it matters.

Test yourself

After logrotate renames app.log to app.log.1, the app keeps writing into app.log.1. Why?

  1. logrotate bug
  2. The app holds an open file descriptor; renaming the file doesn't change where the descriptor points
  3. The disk is full
  4. compress was not set

What is the risk of copytruncate?

  1. It needs root
  2. Lines written between the copy and the truncate are lost
  3. It only works with gzip
  4. It deletes all old logs

Why combine compress with delaycompress?

  1. To save more space
  2. The most recent rotated file stays uncompressed, so a process still writing to it (or a tail -f) isn't broken
  3. gzip is slow
  4. It is required by notifempty