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
| Option | What it does |
|---|---|
-d | debug: does nothing, only prints what would be done (implies -v) |
-v | print verbosely what's happening |
-f | force rotation even if it's not due yet |
-s file | your own state file instead of /var/lib/logrotate/status |
-l file | write a log of its work to a file (or syslog) |
-m command | what to send mail with (for mail) |
When to rotate
| Directive | Meaning |
|---|---|
daily / weekly / monthly / yearly | by time since the last rotation |
hourly | once an hour — only if logrotate itself runs every hour |
size 100M | when the file is larger than the size (time is ignored); units k, M, G |
minsize 1M | by time, but only if the file is at least this size |
maxsize 500M | by time or earlier if the file exceeds the size |
notifempty / ifempty | don't touch / do touch an empty file |
missingok | a missing file isn't an error |
How many to keep and where
| Directive | Meaning |
|---|---|
rotate 7 | keep 7 old copies; rotate 0 — delete old ones right away |
maxage 30 | delete copies older than 30 days |
olddir directory | put copies in another directory (on the same filesystem) |
dateext | a date suffix (app.log-20250115) instead of a number |
dateformat -%Y%m%d-%s | date format for dateext |
extension .log | keep the extension at the end: app-20250115.log |
start 0 | the number to start from (default 1) |
Compression
| Directive | Meaning |
|---|---|
compress / nocompress | compress old copies (gzip by default) |
delaycompress | compress starting next time: .1 stays uncompressed, the service may still be writing to it |
compresscmd, compressext, compressoptions | a different compressor: compresscmd /usr/bin/xz, compressext .xz |
How to get a new file
| Directive | Meaning |
|---|---|
create 0640 user group | after renaming, create a new file with these permissions and owner |
nocreate | don't create a new file |
copytruncate | copy the log and truncate the original — for programs that can't reopen the file |
copy | only copy, leave the original alone |
su user group | rotate as this user — needed if the log directory isn't owned by root |
Scripts
| Directive | When it runs |
|---|---|
prerotate … endscript | before rotation |
postrotate … endscript | after rotation — usually where you signal the service to reopen its log |
firstaction / lastaction … endscript | once before / after processing all files of the rule |
sharedscripts | pre/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,
logrotaterecords "rotated now" and waits for the next period. Use-ffor testing. -ddoes nothing — it's only a dry run. To see the result you need-f(and-v).- Without
postrotateorcopytruncatethe service writes to the old file. Renaming doesn't change the open descriptor: the process keeps writing toapp.log.1, and the newapp.logstays empty. copytruncatecan lose lines written between the copy and the truncation. If the service can reopen its logs,create+postrotateis better.notifemptywins even over-f: an empty log isn't rotated.- A
*pattern catches already rotated copies./var/log/app/*turnsapp.log.1intoapp.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
sudirective. - A standalone config doesn't inherit
/etc/logrotate.conf. When you runlogrotate my.conf, the globalcreate,weeklyand the rest don't apply — put everything you need in the rule. - The size is written without
B:size 10M, not10MB— 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).