Linux Command Reference ยท Packages and Services
Linux logrotate command
Reference for logrotate: configuration blocks, rotate, size, daily, compress, postrotate scripts, dry runs with -d and forcing rotation with -f.
logrotate keeps log files from growing without limit. At each run it looks at the files listed in its configuration, and when a log is old or big enough it renames it (app.log becomes app.log.1), optionally compresses older copies, deletes the oldest ones, and tells the program to open a fresh file.
It does not run as a daemon: a daily cron job or systemd timer calls logrotate /etc/logrotate.conf. That main file sets defaults and includes /etc/logrotate.d/*, where packages (and you) drop one small file per application.
logrotate -d /etc/logrotate.conf # dry run: show what would happen sudo logrotate -f /etc/logrotate.d/app # force a rotation now
Try it
logrotate is usually not installed in the sandbox, so the first example tests for it and runs it on a private config and state file only when it exists. The second one reproduces the rotation scheme in plain shell so you can see the renaming.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
logrotate [OPTION]... CONFIG_FILE
/var/log/app/*.log {
weekly
rotate 8
compress
delaycompress
missingok
notifempty
create 0640 app adm
postrotate
systemctl kill -s HUP app.service
endscript
}
| Directive | Description |
|---|---|
daily, weekly, monthly, yearly |
Rotate by age. |
size N (100k, 10M) |
Rotate when the file is bigger than N (replaces the time rule). |
rotate N |
Keep N old logs; older ones are deleted. |
compress, delaycompress |
Compress rotated files; leave the newest rotated file uncompressed for one more cycle. |
create MODE USER GROUP |
Create a new empty log after rotating. |
copytruncate |
Copy the log and truncate the original (for programs that cannot reopen files). |
missingok, notifempty |
No error when the log is absent; skip empty logs. |
dateext |
Use a date suffix instead of numbers. |
postrotate ... endscript |
Shell commands to run after rotation (also prerotate). |
su USER GROUP |
Rotate as this user (needed for logs in directories others can write). |
| Option | Description |
|---|---|
-d |
Debug: print what would be done, change nothing. |
-f |
Force rotation even if the rules say no. |
-v |
Verbose. |
-s FILE |
Use a different state file (default /var/lib/logrotate/status). |
Returns: 0 on success, 1 if any problem occurred (a bad config or a failed script).
Usage notes
- Test first:
logrotate -d fileshows decisions without touching anything. Combine with-vto see why a log is skipped. - Programs must reopen the file. After
mv, the program still writes to the old (renamed) file until it reopens. Send a signal inpostrotate(oftenHUP), or usecopytruncatewhen it cannot. copytruncatecan lose lines written between the copy and the truncation. Prefer a reload when the program supports it.- State file: logrotate remembers the last rotation time per file; delete an entry there to make it forget.
- Size and time rules do not combine.
sizeoverridesdaily; usemaxsizeorminsizeto mix them. - Permissions: config files must not be writable by others, and logs in group-writable folders need the
sudirective, otherwise logrotate refuses to rotate them. - Wildcards (
*.log) rotate every match under the same rules; a failing script runs for every file unless you addsharedscripts. - Journald manages its own size (
SystemMaxUse); logrotate is for plain-text logs.
Related
crontab,journalctl,logger,truncate- Lessons: Disks and logs, Logging and journald