AlmaLinux 9: rotate your application logs
Test scope
Section titled “Test scope”Executed in an almalinux:9 container (AlmaLinux 9.8): installing logrotate, writing /etc/logrotate.d/myapp, logrotate -d, logrotate -f, ls -l. Log: plans/content-expansion/evidence/logs/alma-log-rotation.log.
Automatic cron.daily invocation, multi-day behaviour and SELinux-related failures were not tested here.
What the system already rotates
Section titled “What the system already rotates”ls /etc/logrotate.d/ | head -5head -20 /etc/logrotate.confAlmaLinux 9 ships snippets such as btmp, wtmp and dnf under /etc/logrotate.d/; the main /etc/logrotate.conf only carries global defaults. Put application configuration in /etc/logrotate.d/, not in the main file.
Write an app snippet
Section titled “Write an app snippet”/var/log/myapp/*.log { daily rotate 3 compress missingok notifempty copytruncate}| Directive | Effect |
|---|---|
daily | Rotate daily (or weekly, size 50M) |
rotate 3 | Keep 3 older files |
compress | gzip the rotated file |
missingok / notifempty | No error when absent; skip empty files |
copytruncate | Copy then truncate, so the app need not reopen its handle |
Dry-run, then force once
Section titled “Dry-run, then force once”sudo logrotate -d /etc/logrotate.d/myappconsidering log /var/log/myapp/app.logCreating new state Now: 2026-10-18 17:54 Last rotated at 2026-10-18 17:00 log does not need rotating (log has already been rotated)-d prints only and changes nothing.
sudo logrotate -f /etc/logrotate.d/myappls -l /var/log/myapp/-rw-r--r-- 1 root root 0 Oct 18 17:54 app.log-rw-r--r-- 1 root root 4023 Oct 18 17:54 app.log.1.gzThe original is truncated and the content moved into .1.gz — that is copytruncate.
Common traps
Section titled “Common traps”copytruncateloses lines written between copy and truncate; prefercreateplus apostrotatesignal when the app can reopen its file;sizewithout a time directive triggers regardless of the clock; do not mix and expect once a day;- Wrong directory ownership: the file created after
copytruncatemay end up owned by root; addsu myapp myapp.
Practice: keep 3 rotations and verify
Section titled “Practice: keep 3 rotations and verify”sudo mkdir -p /var/log/lab && sudo sh -c 'head -c 200000 /dev/urandom > /var/log/lab/a.log'sudo tee /etc/logrotate.d/lab >/dev/null <<'EOF'/var/log/lab/*.log { daily rotate 3 compress copytruncate }EOFsudo logrotate -f /etc/logrotate.d/labsudo logrotate -f /etc/logrotate.d/labls /var/log/lab/Checks:
a.log.1.gzanda.log.2.gzexista.logis 0 bytes- After five forced runs, no more than 3
*.gzfiles remain
常见问题
Why does `logrotate -d` say "already been rotated"?
It reads the state file and decides no rotation is due; `-d` is a dry-run and does not update state. Use `-f` to force one run when you want to validate the configuration now.
copytruncate or create?
If the application can reopen its log file (after a signal), use create with postrotate. Use copytruncate only when it cannot reopen (some Java apps), accepting that lines written between copy and truncate are lost.
Why did the rotated file end up owned by root?
copytruncate runs as root and creates the new file as root. Add `su <user> <group>` so logrotate acts as the app user, or chown in postrotate.
Next steps
Section titled “Next steps”- Packages and transactions: dnf transaction rollback
- Host checks: 7 checks after install
- Repositories: AlmaLinux repositories