Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
/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
}-d = dry run (shows what it would do); -f = force a rotation now.
Step by step
- How it runs: a daily systemd timer (
logrotate.timer) or cron (/etc/cron.daily/logrotate) reads/etc/logrotate.confand every file in/etc/logrotate.d/.
- The options:
| Option | Means |
|---|---|
daily / weekly / size 100M | When to rotate (maxsize 500M = also earlier if big). |
rotate 14 | Keep 14 old files. |
compress + delaycompress | gzip old logs, but not the newest old one (the app may still finish writing). |
missingok, notifempty | No error if missing; skip empty files. |
create 0640 shop shop | New empty file with owner and mode. |
postrotate … endscript | Tell the app to reopen its log (reload or a signal). |
copytruncate | Copy, then empty the file — for apps that can not reopen logs (may lose a few lines). |
- 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. - 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- Status — when each log was rotated last:
grep shop /var/lib/logrotate/logrotate.status- 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.
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.
Common problems
| Problem | Cause | Fix |
|---|---|---|
The app keeps writing to app.log.1 | It did not reopen the file. | postrotate reload/signal, or copytruncate. |
error: skipping … because parent directory has insecure permissions | The 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 rotation | A deleted file is still open by a process. | lsof +L1; restart/reload that process. |
| The rule never runs | Syntax error stops the whole file, or the timer is off. | logrotate -d; systemctl status logrotate.timer. |
| Logs grow fast between daily runs | Very busy app. | maxsize 500M (and run logrotate hourly). |
| Lines missing at rotation time | copytruncate race. | Prefer reopen via postrotate. |
Go further
- Command pages: logrotate, lsof, journalctl.
- Next how-tos: Keep the journal after reboot and limit its size, Check disk space and find what fills the disk, See the logs of a container, Clean temporary files with systemd-tmpfiles.
- Learn it: RHCSA — logrotate, RH134 — logrotate, logrotate in daily DevOps.
- Practise: Lab: a day on call — service, logs, backup, cron and logrotate.
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).