الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

logrotate

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

المستوى
امتحان RHCSA
المجموعة
السجلات
يحتاج root
نعم
الحزمة
logrotate

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

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.

طريقة الكتابة

logrotate [OPTION]... CONFIG_FILE...

الأجزاء الرئيسية

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).

الخيارات 11

الأكثر استخدامًا

-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.

أمثلة 10

$ مستخدم عادي# بصلاحية root (أو مع 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.

الملفات

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

رموز الخروج

0 :: Done. 1 :: Error.

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man 8 logrotate