Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

logrotate

Rotate, compress and delete old log files by the rules in /etc/logrotate.conf and /etc/logrotate.d/.

Niveau
RHCSA-examen
Groep
Logs
Root nodig
Ja
Pakket
logrotate

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

logrotate renames a log (like messages → messages-20261025), starts a new empty one, compresses old files and deletes the oldest. On RHEL the logrotate.timer runs it once a day.

Main settings are in /etc/logrotate.conf; packages put their own rules in /etc/logrotate.d/. Test a rule with -d (debug, changes nothing) and force a rotation with -f.

Zo schrijf je het

logrotate [OPTION]... CONFIG_FILE...

Belangrijkste delen

daily, weekly, monthly, yearly
When to rotate.
size SIZE / minsize / maxsize
Rotate by size (like 100M).
rotate N
Keep N old files.
compress / delaycompress
gzip old files / wait one cycle.
dateext
Add the date to the name (RHEL default).
missingok / notifempty
No error if missing / skip empty logs.
create MODE OWNER GROUP
Make the new log with these rights.
copytruncate
Copy and empty the file (for programs that keep it open).
postrotate … endscript
Commands after the rotation, like a reload.
sharedscripts
Run the scripts once for all files of the rule.
maxage DAYS
Delete rotated files older than this.
su USER GROUP
Rotate as this user (for folders not owned by root).

Opties 11

Meest gebruikt

-d, --debug
Test mode: show what would happen, change nothing.
-f, --force
Rotate now, even if it is not time yet.
-v, --verbose
Show details.
-s, --state FILE
Use another state file.
--skip-state-lock
Do not lock the state file.
--wait-for-state-lock
Wait for the state file lock.
-l, --log FILE
Write a log of what happens.
-m, --mail COMMAND
The mail command for mail settings.
--usage
Short usage.
-?, --help
Show help.
--version
Show the version.

Voorbeelden 10

$ als gewone gebruiker# als root (of met sudo)

  1. 1Show the main settings

    grep -v '^#' /etc/logrotate.conf | grep .
    weekly
    rotate 4
    create
    dateext
    include /etc/logrotate.d

    Defaults for all rules.

  2. 2Rules of a package

    cat /etc/logrotate.d/httpd

    Includes a postrotate reload of httpd.

  3. 3Write a rule for your app

    cat /etc/logrotate.d/app
    /var/log/app.log {
        weekly
        rotate 8
        compress
        missingok
        notifempty
        create 0640 root root
    }

    Keeps 8 weeks of compressed logs.

  4. 4Test a rule

    logrotate -d /etc/logrotate.d/app

    Shows the plan; nothing changes.

  5. 5Force a rotation now

    logrotate -fv /etc/logrotate.d/app

    Check the result with ls -l /var/log/app.log*.

  6. 6Rotate by size

    cat /etc/logrotate.d/bigapp
    /var/log/bigapp/*.log {
        size 200M
        rotate 5
        compress
        copytruncate
    }

    For apps that cannot reopen their logs.

  7. 7When did it run last?

    systemctl list-timers logrotate.timer
    cat /var/lib/logrotate/logrotate.status | head

    The state file stores the last rotation per log.

  8. 8Keep logs for compliance

    sed -i 's/^rotate 4/rotate 52/' /etc/logrotate.conf

    One year of weekly logs (check the disk space; also send logs to a central server).

  9. 9Find rules with errors

    logrotate -d /etc/logrotate.conf 2>&1 | grep -i error

    Wrong owners or rights stop the rotation.

  10. 10Reload a service after rotation

    grep -A3 postrotate /etc/logrotate.d/nginx

    Without it, the program writes to the renamed old file.

Bestanden

/etc/logrotate.conf
Main settings.
/etc/logrotate.d/
Rules per program.
/var/lib/logrotate/logrotate.status
Last rotation times.

Exitcodes

0Done.
1Error.

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man 8 logrotate