logrotate Konfiguration und Ausführung

8625
pnongrata

Ich versuche zu konfigurieren logrotate, Protokolldateien jeden Tag um Mitternacht zu drehen und zu löschen, habe sie jedoch noch nie verwendet. Also: (a) Ich möchte bestätigen, dass meine Konfiguration korrekt ist, und (b) ich brauche Hilfe, um die tägliche Ausführung um Mitternacht zu automatisieren.

Meine Software erzeugt eine große Protokolldatei unter /abs/path/to/log/myapp-log.txt. Folgendes würde ich gerne machen:

  • Zeitstempel für jede Protokolldatei mit der Datumserweiterung im Namen
  • Haben Sie /abs/path/to/log/zu jedem Zeitpunkt nur eine Logdatei ; Löschen Sie also jedes Mal, wenn das Protokoll gedreht und eine neue Protokolldatei erstellt wird

logrotate.conf:

/abs/path/to/log/myapp-log.txt { daily copytruncate create 700 myUser myGroup dateext maxage 1 missingok } 

Ein paar Dinge, bei denen ich mir nicht sicher bin:

  • Muss ich angeben, dailyob ich eine maxagevon 1 eingebe ?
  • Was macht genau das missingok? Etwas über das Ignorieren einer Systemwarnung, wenn keine Protokolldatei vorhanden ist? Was passiert, wenn ich keine Angabe mache missingokund die Protokolldatei nicht vorhanden ist?
  • Brauche ich überhaupt, copytruncatewenn ich daily/ spezifiziere maxage 1? Diese drei Einstellungen wirken ein wenig überflüssig, sind sich jedoch nicht sicher, welche Rolle sie jeweils spielen.

Zweitens, wie kann ich sicherstellen, dass dies jeden Tag um Mitternacht ausgeführt wird? Cron? Ich frage nur, weil ich irgendwo gelesen habe, /etc/cron.daily/logrotatedas jeden Tag automatisch läuft, aber nicht sicher ist, wie das konfigurierbar ist. Danke im Voraus.

3

1 Antwort auf die Frage

10
John Siu
  1. Do I need to specify daily if I'm specifying a maxage of 1?

    Yes, you need daily as you want this section to execute everyday. This control how execution frequency of the section, while maxage control how long rotated files are kept. They are 2 different things.

  2. What exactly is missingok doing?

    missingok means logrotate will not complain/generate error if the log file for rotation does not exist. If this is not specified and the log file intended for rotation is missing, logrotate will generate an error log.

  3. Do I even need copytruncate if I'm specifying daily/maxage 1?

    If copytruncate is working for you now, don't change it. It apply to original log file

    copytruncate instruct logrotate to creates the copy of the original file (i.e rotate the original log file) and truncates the original file to zero byte size. This helps the respective service that belongs to that log file can write to the proper file.

  4. To run logrotate at mid-night

    There are 2 methods to do it.

    • Method 1 - Change when daily cron is run

      Look for following lines in /etc/crontab

      25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 

      Change 25 6 (means 6:25AM), to 0 0. This also change ALL daily cron job starting time to mid-night.

    • Method 2 - Use custom crontab line

      Move logrotate out of default daily cron schedule

      mv /etc/cron.daily/logrotate /etc/logrotate.cronjob 

      Create custom cron job. Add following line into /etc/crontab

      0 0 * * * root /etc/logrotate.cronjob