logrotate ist das Standard-Tool für Log-Rotation unter Linux. Es komprimiert, archiviert und löscht alte Log-Dateien automatisch.
Installation
# Meist vorinstalliert
# Debian/Ubuntu
apt install logrotate
# RHEL/CentOS
dnf install logrotate
# Version prüfen
logrotate --versionGrundlagen
Konfigurationsdateien
# Hauptkonfiguration
/etc/logrotate.conf
# Anwendungs-spezifisch
/etc/logrotate.d/
# Statusdatei
/var/lib/logrotate/statusHaupt-Konfiguration
# /etc/logrotate.conf
# Wöchentliche Rotation
weekly
# 4 Versionen behalten
rotate 4
# Rotierte Logs erstellen
create
# Datum im Dateinamen
dateext
# Komprimieren
compress
# Inkludieren
include /etc/logrotate.dKonfigurationsoptionen
Rotation-Intervall
# Täglich
daily
# Wöchentlich
weekly
# Monatlich
monthly
# Jährlich
yearlyGrößenbasiert
# Mindestgröße für Rotation
size 100M
# Mindestgröße + Zeit
minsize 50M
# Maximalgröße (sofortige Rotation)
maxsize 500MAnzahl und Kompression
# Anzahl Versionen behalten
rotate 7
# Alte Logs löschen
rotate 0
# Komprimieren
compress
# Verzögerte Kompression
delaycompress
# Komprimierungs-Programm
compresscmd /usr/bin/xz
compressext .xz
compressoptions -9Datei-Erstellung
# Neue Log-Datei erstellen
create 640 root adm
# Keine neue Datei erstellen
nocreate
# Alte umbenennen, neue wird von App erstellt
copytruncateBedingungen
# Nur wenn nicht leer
notifempty
# Fehler ignorieren
missingok
# Rotation erzwingen
ifemptyBeispiel-Konfigurationen
Nginx
# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 www-data adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)
endscript
}Apache
# /etc/logrotate.d/apache2
/var/log/apache2/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 640 root adm
sharedscripts
postrotate
if /etc/init.d/apache2 status > /dev/null ; then
/etc/init.d/apache2 reload > /dev/null
fi
endscript
}Syslog
# /etc/logrotate.d/rsyslog
/var/log/syslog
/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/cron.log
{
rotate 4
weekly
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
/usr/lib/rsyslog/rsyslog-rotate
endscript
}Anwendungs-Logs
# /etc/logrotate.d/myapp
/var/log/myapp/*.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
copytruncate
dateext
dateformat -%Y%m%d
}MySQL
# /etc/logrotate.d/mysql
/var/log/mysql/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 640 mysql adm
sharedscripts
postrotate
if [ -x /usr/bin/mysqladmin ]; then
/usr/bin/mysqladmin flush-logs
fi
endscript
}Scripts
prerotate/postrotate
/var/log/app/*.log {
daily
rotate 7
compress
sharedscripts
prerotate
# Vor der Rotation
/usr/local/bin/pre-rotate.sh
endscript
postrotate
# Nach der Rotation
systemctl reload app
endscript
}firstaction/lastaction
/var/log/app/*.log {
daily
rotate 7
compress
sharedscripts
firstaction
# Ganz am Anfang (einmal)
echo "Starting rotation" | logger
endscript
lastaction
# Ganz am Ende (einmal)
echo "Finished rotation" | logger
endscript
}Manuell ausführen
# Test-Lauf (kein echter Rotate)
logrotate -d /etc/logrotate.conf
# Erzwungene Rotation
logrotate -f /etc/logrotate.conf
# Verbose
logrotate -v /etc/logrotate.conf
# Einzelne Konfiguration
logrotate -f /etc/logrotate.d/nginx
# Mit anderem Status-File
logrotate -s /tmp/logrotate.status /etc/logrotate.confCron-Job
# Standard (meist vorhanden)
# /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT - rotation failed"
fi
exit $EXITVALUESystemd-Timer (modern)
# /etc/systemd/system/logrotate.timer
[Unit]
Description=Daily rotation of log files
[Timer]
OnCalendar=daily
AccuracySec=1h
Persistent=true
[Install]
WantedBy=timers.targetTroubleshooting
Debug-Modus
# Verbose Test
logrotate -dv /etc/logrotate.d/nginx
# Ausgabe:
# rotating pattern: /var/log/nginx/*.log after 1 days
# switching euid to 0 and egid to 4
# considering log /var/log/nginx/access.log
# log needs rotatingStatus prüfen
# Status-Datei anzeigen
cat /var/lib/logrotate/status
# Ausgabe:
# logrotate state -- version 2
# "/var/log/nginx/access.log" 2026-1-26-10:0:0
# "/var/log/nginx/error.log" 2026-1-26-10:0:0Häufige Probleme
# "error: skipping because parent directory has insecure permissions"
# → Berechtigungen prüfen: chmod 755 /var/log/myapp
# Log wird nicht rotiert
# → Status-Datei löschen: rm /var/lib/logrotate/status
# → Oder: logrotate -f
# "error: stat of /var/log/myapp/*.log failed"
# → missingok hinzufügenWildcard und Patterns
# Mehrere Dateien
/var/log/app/*.log
/var/log/app/**/*.log # Rekursiv (nicht Standard)
# Mehrere Pfade
/var/log/app1/*.log
/var/log/app2/*.log
{
daily
rotate 7
}
# Ausschluss (nicht nativ unterstützt)
# Workaround: explizite ListeErweiterte Optionen
# Datum-Format
dateext
dateformat -%Y%m%d-%H%M%S
# Alte Logs woanders speichern
olddir /var/log/archive
createolddir 755 root root
# Mail bei Löschen
mail admin@example.de
mailfirst # Oder maillast
# Taboo-Extensions ignorieren
tabooext + .bak .old
# Su für Rotation
su root admZusammenfassung
| Option | Beschreibung | |--------|--------------| | daily/weekly/monthly | Intervall | | rotate N | Anzahl Versionen | | compress | Komprimieren | | delaycompress | Verzögert komprimieren | | copytruncate | Kopieren statt umbenennen | | create | Neue Datei erstellen | | missingok | Fehler ignorieren | | notifempty | Nicht wenn leer | | sharedscripts | Scripts einmal ausführen | | postrotate/endscript | Nach Rotation |
| Befehl | Funktion | |--------|----------| | logrotate -d | Debug/Test | | logrotate -f | Erzwingen | | logrotate -v | Verbose | | logrotate -s | Status-Datei |
| Datei | Beschreibung | |-------|--------------| | /etc/logrotate.conf | Hauptkonfiguration | | /etc/logrotate.d/ | App-Konfigurationen | | /var/lib/logrotate/status | Status |
Fazit
logrotate ist unverzichtbar für Log-Verwaltung. Regelmäßige Rotation verhindert volle Festplatten. postrotate-Scripts signalisieren Anwendungen. delaycompress erleichtert Debugging. Für Container und moderne Apps sind auch andere Lösungen wie journald relevant.