Logrotate Konfiguration Anleitung
Logrotate Konfiguration
Logrotate rotiert, komprimiert und löscht Log-Dateien automatisch. Ohne Logrotate würden Log-Dateien die Festplatte füllen und den Server lahmlegen.
Grundlagen
# Logrotate ist meist vorinstalliert
sudo apt install logrotate -y
# Hauptkonfiguration
cat /etc/logrotate.conf
# Anwendungsspezifische Configs
ls /etc/logrotate.d/
Konfigurationssyntax
/var/log/myapp/*.log {
daily # Täglich rotieren
rotate 7 # 7 Versionen behalten
compress # Komprimieren
delaycompress # Erst nach einem Zyklus
missingok # Kein Fehler wenn Datei fehlt
notifempty # Leere Dateien ignorieren
create 640 root adm # Neue Datei mit Rechten
postrotate
systemctl reload myapp > /dev/null 2>&1 || true
endscript
}
Wichtige Optionen
| Option | Beschreibung |
|---|---|
daily/weekly/monthly |
Rotationsintervall |
rotate N |
Anzahl behalten |
compress |
Mit gzip komprimieren |
size N |
Bei Größe rotieren (z.B. 100M) |
maxsize N |
Maximale Größe |
copytruncate |
Kopieren statt verschieben |
Beispiel: Nginx
sudo nano /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 www-data adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
Beispiel: Eigene Anwendung
sudo nano /etc/logrotate.d/myapp
/var/log/myapp/*.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 0644 www-data www-data
size 100M
postrotate
systemctl reload myapp > /dev/null 2>&1 || true
endscript
}
Testen und Debug
# Konfiguration testen (Trockenlauf)
sudo logrotate -d /etc/logrotate.d/myapp
# Manuell ausführen
sudo logrotate -f /etc/logrotate.d/myapp
# Status-Datei anzeigen
cat /var/lib/logrotate/status
Cron-Job prüfen
cat /etc/cron.daily/logrotate
Weitere Hilfe
- 📖
man logrotate - 📧 E-Mail: support@enjyn.de