On this page
- What does logrotate do and when does it run?
- How do I write a rule for my own application?
- How do I test a rule safely before it runs at night?
- Should I rotate by time or by size?
- What is the difference between create and copytruncate?
- Where does logrotate remember what it already did?
- How do Docker containers and web servers fit in?
- Why did a rotation fail and how do I fix it?
- Questions
What does logrotate do and when does it run?
logrotate renames a growing log file, starts a fresh one, compresses older copies and deletes the oldest, all according to rules you write. It is installed on Ubuntu, Debian, Rocky Linux and AlmaLinux images, and a systemd timer runs it daily:
logrotate --version
systemctl list-timers logrotate.timer
systemctl status logrotate.service --no-pagerThe global defaults are in /etc/logrotate.conf, and each package drops its own rule into /etc/logrotate.d/ (Nginx, rsyslog, apt or dnf, and many more). Your own apps need a rule too; otherwise a log written with >> grows until the disk is full, which is one of the most common reasons a small server stops working. The disk usage guide shows how to spot such files, and the Usage graphs tab in the console helps you notice a sudden jump in disk activity.
How do I write a rule for my own application?
Create one file per app. This example covers every .log file of an app running as the deploy user:
sudo nano /etc/logrotate.d/myapp/var/log/myapp/*.log {
daily
rotate 14
missingok
notifempty
compress
delaycompress
dateext
create 0640 deploy deploy
su deploy deploy
sharedscripts
postrotate
systemctl reload myapp.service > /dev/null 2>&1 || true
endscript
}Line by line: rotate once a day, keep 14 old copies, do not complain if the files are missing, skip empty files, gzip old copies but leave yesterday uncompressed (handy for tools still reading it), add the date to the name, and create the new file with the right owner. su is required when the folder is writable by a non root user; without it logrotate refuses to touch the files for safety. sharedscripts runs the reload once for all matched files instead of once per file.
How do I test a rule safely before it runs at night?
The debug flag walks through the rule and prints what it would do without changing anything:
sudo logrotate -d /etc/logrotate.d/myappLook for lines such as rotating pattern, considering log and log does not need rotating. Errors about insecure permissions or a missing su line appear here too. When the output looks right, force one real rotation and inspect the folder:
sudo logrotate -f -v /etc/logrotate.d/myapp
ls -l /var/log/myapp/You should now see the fresh file plus a dated copy. Do not leave -f in any script: it rotates even when the rule says it is not due, which defeats the schedule.
Should I rotate by time or by size?
Use time based rotation (daily, weekly, monthly) for logs with a steady rate, because the files line up with days and are easy to search. Use size for bursty logs:
size 100M: rotate only when the file passes 100 MB, ignoring the calendar.maxsize 100Mwithdaily: rotate daily, or earlier if the file passes 100 MB at a run.minsize 1Mwithdaily: rotate daily, but only files larger than 1 MB.maxage 30: delete rotated copies older than 30 days whatever the count.
Size checks only happen when logrotate runs, so with the daily timer a file can still grow a lot in one day. For a very noisy app, run logrotate hourly through a timer override:
sudo systemctl edit logrotate.timer[Timer]
OnCalendar=
OnCalendar=hourlyThe empty OnCalendar= line clears the default before setting the new value. Apply with sudo systemctl daemon-reload; the cron and timers guide covers timer syntax in more depth.
What is the difference between create and copytruncate?
They decide how the program keeps writing after rotation. With create, logrotate renames the file and creates a new empty one; the program must reopen its log, which is why the postrotate block reloads it or sends a signal (Nginx uses USR1, rsyslog HUP). This is the clean method and loses nothing.
Some apps cannot reopen their logs, for example a script that holds the file open forever. For those, copytruncate copies the file and then truncates the original in place. The program keeps writing to the same handle, but lines written in the short gap between copy and truncate can be lost. Prefer create with a reload whenever the app supports it, and use copytruncate only as a fallback. Apps that log to the journal instead of files need no logrotate at all; journald handles its own limits, explained in the journalctl guide.
Where does logrotate remember what it already did?
In a state file that stores the last rotation date for each log:
sudo cat /var/lib/logrotate/status # Ubuntu, Debian
sudo cat /var/lib/logrotate/logrotate.status # Rocky, AlmaIf a log was never rotated, check that its path appears in this file after a run. When you test rules on a copy of the configuration, point logrotate at a scratch state file so your experiments do not change the real schedule:
sudo logrotate -v -s /tmp/lr-test.state /etc/logrotate.d/myappDeleting the real state file is harmless but makes logrotate treat every log as new, so some files will wait one more cycle before their next rotation.
How do Docker containers and web servers fit in?
Packages such as Nginx and Apache install their own rules, so check /etc/logrotate.d/nginx and adjust the retention there rather than writing a second rule for the same files; two rules for one path cause errors. Container logs are different: Docker keeps them in its own JSON files, and you cap them with the logging options in daemon.json, described in reading and limiting Docker logs. Logs collected from other servers by a central rsyslog host need their own rule with a HUP to rsyslog after rotation.
Why did a rotation fail and how do I fix it?
- "skipping because parent directory has insecure permissions": add
su user groupto the rule, matching the owner of the folder. - "duplicate log entry": two rules match the same file. Run
grep -r myapp /etc/logrotate.d/and remove one. - App keeps writing to the rotated file: the postrotate step did not make it reopen the log. Fix the reload command or switch to
copytruncate. - Nothing happens at night: check
systemctl list-timersandjournalctl -u logrotate.servicefor errors from any rule; one broken rule can stop others in the same run. - Disk still fills: count the old copies with
ls /var/log/myapp | wc -land lowerrotateor addmaxage.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
| Plan | Fits because | Size | Monthly | Order from |
|---|---|---|---|---|
| S1-2 | any plan, logrotate ships with the OS | 1 vCPU, 2 GB RAM, 55 GB SSD | $6.00/mo | $72.00 for 12 months |
Questions about Rotate logs with logrotate
How often does logrotate run by default?
Once a day through logrotate.timer on Ubuntu, Debian, Rocky and Alma. Rules marked weekly or monthly are checked daily and rotated only when due.
Does logrotate handle the systemd journal?
No. The journal has its own size limits set in journald.conf. logrotate is only for plain text files.
Can I keep compressed logs for a year?
Yes, with daily and rotate 365, or weekly and rotate 52. Check the disk budget first, and consider shipping old logs off the server with your backup routine.
Why use delaycompress?
It keeps the newest rotated copy uncompressed for one more cycle, which helps when a program still writes a few lines to the old file right after rotation.
Do I need sudo for logrotate?
Yes for system logs, because the files and state belong to root. A user can run logrotate on personal logs with a private state file using -s.
Can logrotate run a script after each rotation?
Yes. Anything between postrotate and endscript runs as a shell script, for example to upload the rotated file to another server.
More in Monitoring and logs
- Netdata real-time monitoring
- Uptime Kuma status page
- Prometheus and node_exporter
- Grafana dashboards
- Zabbix monitoring
- Nagios Core
- Beszel lightweight monitoring
- Loki and Alloy for central logs
- Cacti graphs with SNMP
- Monit for self-healing services
- btop, htop and other live monitors
- Central logs with rsyslog
- Read logs with journalctl
- Graylog log management
- Observium network monitoring
- Vector log and metrics pipeline