日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解Linux日志處理命令logrotate

Linux使用某些軟件的時候會產(chǎn)生日志文件,而這些軟件本身對日志不進行分割或者壓縮處理,久而久之會導(dǎo)致日志文件異常巨大,影響機器性能,配置不高的VPS上尤為嚴(yán)重。而logrotate就是管理這些日志文件的神器,可以對單個日志文件或者某個目錄下的文件按時間/大小進行切割,壓縮操作;指定日志保存數(shù)量;還可以在切割之后運行自定義命令。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、重慶小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了通道免費建站歡迎大家使用!

基本功能

logrotate 實用程序在管理日志方面非常出色。它可以輪轉(zhuǎn)日志、壓縮日志、通過電子郵件發(fā)送日志、刪除日志、歸檔日志,并在你需要時開始記錄最新的。

運行 logrotate 非常簡單——只需要運行 logrotate -vs state-file config-file。在上面的命令中,v 選項開啟詳細(xì)模式,s 指定一個狀態(tài)文件,最后的 config-file 是配置文件,你可以指定需要做什么。

實戰(zhàn)演練

讓我們看看在我們的系統(tǒng)上靜默運行的 logrotate 配置,它管理我們在 /var/log 目錄中找到的大量日志。查看該目錄中的當(dāng)前文件。你是否看到很多 *.[number].gz 文件?這就是 logrotate 正在做的。你可以在 /etc/logrotate.d/rsyslog 下找到此配置文件。我的配置文件如下:

/var/log/syslog{        rotate 7        daily        missingok        notifempty        delaycompress        compress        postrotate                reload rsyslog > /dev/null 2>&1 || true        endscript}/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/lpr.log/var/log/cron.log/var/log/debug/var/log/messages{        rotate 4        weekly        missingok        notifempty        compress        delaycompress        sharedscripts        postrotate                reload rsyslog > /dev/null 2>&1 || true        endscript}

該文件首先定義了輪轉(zhuǎn) /var/log/syslog 文件的說明,這些說明包含在后面的花括號中。以下是它們的含義:

  • rotate 7: 保留最近 7 次輪轉(zhuǎn)的日志。然后開始刪除超出的。
  • daily: 每天輪轉(zhuǎn)日志,與
    rotate 7 一起使用,這意味著日志將保留過去 7 天。其它選項是每周、每月、每年。還有一個大小參數(shù),如果日志文件的大小增加超過指定的限制(例如,大小 10k、大小 10M、大小 10G 等),則將輪轉(zhuǎn)日志文件。如果未指定任何內(nèi)容,日志將在運行
    logrotate 時輪轉(zhuǎn)。你甚至可以在 cron 中運行
    logrotate 以便在更具體的時間間隔內(nèi)使用它。
  • missingok: 如果日志文件缺失也沒關(guān)系。不要驚慌。
  • notifempty: 日志文件為空時不輪轉(zhuǎn)。
  • compress: 開啟壓縮,使用
    nocompress 關(guān)閉它。
  • delaycompress: 如果壓縮已打開,則將壓縮延遲到下一次輪轉(zhuǎn)。這允許至少存在一個輪轉(zhuǎn)但未壓縮的文件。如果你希望昨天的日志保持未壓縮以便進行故障排除,那么此配置會很有用。如果某些程序在重新啟動/重新加載之前可能仍然寫入舊文件,這也很有幫助,例如 Apache。
  • postrotate/endscript: 輪轉(zhuǎn)后運行此部分中的腳本。有助于做清理工作。還有一個
    prerotate/endscript 用于在輪轉(zhuǎn)開始之前執(zhí)行操作。

你能弄清楚下一節(jié)對上面配置中提到的所有文件做了什么嗎?第二節(jié)中唯一多出的參數(shù)是 sharedscripts,它告訴 logrotate 在所有日志輪轉(zhuǎn)完成之前不要運行 postrotate/endscript 中的部分。它可以防止腳本在每一次輪轉(zhuǎn)時執(zhí)行,只在最后一次輪轉(zhuǎn)完成時執(zhí)行。

看點新的東西

我使用下面的配置來處理我系統(tǒng)上的 Nginx 的訪問和錯誤日志。

/var/log/nginx/access.log/var/log/nginx/error.log  {        size 1        missingok        notifempty        create 544 www-data adm        rotate 30        compress        delaycompress        dateext        dateformat -%Y-%m-%d-%s        sharedscripts        extension .log        postrotate                service nginx reload        endscript}

上面的腳本可以使用如下命令運行:

logrotate -vs state-file /tmp/logrotate

第一次運行該命令會給出以下輸出:

reading config file /tmp/logrotateextension is now .logHandling 1 logsrotating pattern: /var/log/nginx/access.log/var/log/nginx/error.log   1 bytes (30 rotations)empty log files are not rotated, old logs are removedconsidering log /var/log/nginx/access.log  log needs rotatingconsidering log /var/log/nginx/error.log  log does not need rotatingrotating log /var/log/nginx/access.log, log->rotateCount is 30Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'dateext suffix '-2021-08-27-1485508250'glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'glob finding logs to compress failedglob finding old rotated logs failedrenaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508250.logcreating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4running postrotate script* Reloading nginx configuration nginx

第二次運行它:

reading config file /tmp/logrotateextension is now .logHandling 1 logsrotating pattern: /var/log/nginx/access.log/var/log/nginx/error.log   1 bytes (30 rotations)empty log files are not rotated, old logs are removedconsidering log /var/log/nginx/access.log  log needs rotatingconsidering log /var/log/nginx/error.log  log does not need rotatingrotating log /var/log/nginx/access.log, log->rotateCount is 30Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'dateext suffix '-2021-08-27-1485508280'glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'compressing log with: /bin/gziprenaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508280.logcreating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4running postrotate script* Reloading nginx configuration nginx

第三次運行它:

reading config file /tmp/logrotateextension is now .logHandling 1 logsrotating pattern: /var/log/nginx/access.log/var/log/nginx/error.log   1 bytes (30 rotations)empty log files are not rotated, old logs are removedconsidering log /var/log/nginx/access.log  log needs rotatingconsidering log /var/log/nginx/error.log  log does not need rotatingrotating log /var/log/nginx/access.log, log->rotateCount is 30Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'dateext suffix '-2021-08-27-1485508316'glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'compressing log with: /bin/gziprenaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508316.logcreating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4running postrotate script* Reloading nginx configuration nginx

狀態(tài)文件的內(nèi)容如下所示:

logrotate state -- version 2"/var/log/nginx/error.log" 2021-08-27-9:0:0"/var/log/nginx/access.log" 2021-08-27-9:11:56

文章題目:詳解Linux日志處理命令logrotate
當(dāng)前網(wǎng)址:http://m.5511xx.com/article/cdeoopi.html