Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 102System · Logs

How to rotate log files with logrotate

An app writes to /var/log/shop/app.log — and after six months the file is 40 GB and the disk is full. logrotate renames logs on a schedule, compresses old ones, deletes the oldest, and tells the app to open a new file. This how-to writes a rule for your own app, tests it without waiting a day, and fixes the classic "the app still writes to the old file".

  • 5 Lessons
  • 9 Commands

$ run as a normal user# run as root (or with sudo)

102.1

The quick answer

/etc/logrotate.d/shop
/var/log/shop/*.log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 0640 shop shop
    sharedscripts
    postrotate
        systemctl reload shop-api >/dev/null 2>&1 || true
    endscript
}
logrotate -d /etc/logrotate.d/shop
logrotate -f /etc/logrotate.d/shop

-d = dry run (shows what it would do); -f = force a rotation now.

102.2

Step by step

  1. How it runs: a daily systemd timer (logrotate.timer) or cron (/etc/cron.daily/logrotate) reads /etc/logrotate.conf and every file in /etc/logrotate.d/.
systemctl list-timers | grep logrotate
  1. The options:
OptionMeans
daily / weekly / size 100MWhen to rotate (maxsize 500M = also earlier if big).
rotate 14Keep 14 old files.
compress + delaycompressgzip old logs, but not the newest old one (the app may still finish writing).
missingok, notifemptyNo error if missing; skip empty files.
create 0640 shop shopNew empty file with owner and mode.
postrotate … endscriptTell the app to reopen its log (reload or a signal).
copytruncateCopy, then empty the file — for apps that can not reopen logs (may lose a few lines).
  1. Tell the app — after the rename the app still writes to the old file (same inode) until it reopens it. Nginx: nginx -s reopen / kill -USR1; many apps: systemctl reload; if the app can do neither: copytruncate.
  2. Test now (quick answer), then look:
ls -l /var/log/shop/
-rw-r----- 1 shop shop       0 Nov 19 10:42 app.log
-rw-r----- 1 shop shop  812345 Nov 19 10:42 app.log.1
-rw-r----- 1 shop shop   81234 Nov 18 03:10 app.log.2.gz
  1. Status — when each log was rotated last:
grep shop /var/lib/logrotate/logrotate.status
  1. Better for new apps: log to stdout/stderr and let journald (with size limits) or the container runtime handle rotation — then no logrotate rule is needed.

102.3

Check that it worked

After a forced rotation: a new empty app.log exists with the right owner, the app writes into it (its size grows), the old files are numbered and compressed, and the number of old files does not grow beyond rotate.

102.4

Common problems

ProblemCauseFix
The app keeps writing to app.log.1It did not reopen the file.postrotate reload/signal, or copytruncate.
error: skipping … because parent directory has insecure permissionsThe log folder is writable by a non-root group/user.Add su shop shop to the rule (or fix the folder rights).
Disk still full after rotationA deleted file is still open by a process.lsof +L1; restart/reload that process.
The rule never runsSyntax error stops the whole file, or the timer is off.logrotate -d; systemctl status logrotate.timer.
Logs grow fast between daily runsVery busy app.maxsize 500M (and run logrotate hourly).
Lines missing at rotation timecopytruncate race.Prefer reopen via postrotate.

102.5

Go further

1Why does an app keep writing into app.log.1 after rotation?

It still has the old file open; it must reopen (reload/signal), or use copytruncate.

2How do you test a logrotate rule without waiting a day?

logrotate -d RULE (dry run), then logrotate -f RULE (force).

All commands in this chapter