自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用 logrotate 命令輪轉(zhuǎn)和歸檔日志

運(yùn)維 系統(tǒng)運(yùn)維
日志非常適合找出應(yīng)用程序在做什么或?qū)赡艿膯?wèn)題進(jìn)行故障排除。幾乎我們處理的每個(gè)應(yīng)用程序都會(huì)生成日志,我們希望我們自己開(kāi)發(fā)的應(yīng)用程序也生成日志。日志越詳細(xì),我們擁有的信息就越多。使用此 Linux 命令保持日志文件更新。

[[430513]]

使用此 Linux 命令保持日志文件更新。

日志非常適合找出應(yīng)用程序在做什么或?qū)赡艿膯?wèn)題進(jìn)行故障排除。幾乎我們處理的每個(gè)應(yīng)用程序都會(huì)生成日志,我們希望我們自己開(kāi)發(fā)的應(yīng)用程序也生成日志。日志越詳細(xì),我們擁有的信息就越多。但放任不管,日志可能會(huì)增長(zhǎng)到無(wú)法管理的大小,反過(guò)來(lái),它們可能會(huì)成為它們自己的問(wèn)題。因此,最好將它們進(jìn)行裁剪,保留我們需要的那些,并將其余的歸檔。

基本功能

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

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

實(shí)戰(zhàn)演練

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

  1. /var/log/syslog
  2. {
  3.         rotate 7
  4.         daily
  5.         missingok
  6.         notifempty
  7.         delaycompress
  8.         compress
  9.         postrotate
  10.                 reload rsyslog > /dev/null 2>&1 || true
  11.         endscript
  12. }
  13.  
  14. /var/log/mail.info
  15. /var/log/mail.warn
  16. /var/log/mail.err
  17. /var/log/mail.log
  18. /var/log/daemon.log
  19. /var/log/kern.log
  20. /var/log/auth.log
  21. /var/log/user.log
  22. /var/log/lpr.log
  23. /var/log/cron.log
  24. /var/log/debug
  25. /var/log/messages
  26.  
  27. {
  28.         rotate 4
  29.         weekly
  30.         missingok
  31.         notifempty
  32.         compress
  33.         delaycompress
  34.         sharedscripts
  35.         postrotate
  36.                 reload rsyslog > /dev/null 2>&1 || true
  37.         endscript
  38. }

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

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

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

看點(diǎn)新的東西

我使用下面的配置來(lái)處理我系統(tǒng)上的 Nginx 的訪問(wèn)和錯(cuò)誤日志。

  1. /var/log/nginx/access.log
  2. /var/log/nginx/error.log  {
  3.         size 1
  4.         missingok
  5.         notifempty
  6.         create 544 www-data adm
  7.         rotate 30
  8.         compress
  9.         delaycompress
  10.         dateext
  11.         dateformat -%Y-%m-%d-%s
  12.         sharedscripts
  13.         extension .log
  14.         postrotate
  15.                 service nginx reload
  16.         endscript
  17. }

上面的腳本可以使用如下命令運(yùn)行:

  1. logrotate -vs state-file /tmp/logrotate

第一次運(yùn)行該命令會(huì)給出以下輸出:

  1. reading config file /tmp/logrotate
  2. extension is now .log
  3.  
  4. Handling 1 logs
  5.  
  6. rotating pattern: /var/log/nginx/access.log
  7. /var/log/nginx/error.log   1 bytes (30 rotations)
  8. empty log files are not rotated, old logs are removed
  9. considering log /var/log/nginx/access.log
  10.   log needs rotating
  11. considering log /var/log/nginx/error.log
  12.   log does not need rotating
  13. rotating log /var/log/nginx/access.log, log->rotateCount is 30
  14. Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'
  15. dateext suffix '-2021-08-27-1485508250'
  16. 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]'
  17. glob finding logs to compress failed
  18. glob finding old rotated logs failed
  19. renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508250.log
  20. creating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4
  21. running postrotate script
  22. * Reloading nginx configuration nginx

第二次運(yùn)行它:

  1. reading config file /tmp/logrotate
  2. extension is now .log
  3.  
  4. Handling 1 logs
  5.  
  6. rotating pattern: /var/log/nginx/access.log
  7. /var/log/nginx/error.log   1 bytes (30 rotations)
  8. empty log files are not rotated, old logs are removed
  9. considering log /var/log/nginx/access.log
  10.   log needs rotating
  11. considering log /var/log/nginx/error.log
  12.   log does not need rotating
  13. rotating log /var/log/nginx/access.log, log->rotateCount is 30
  14. Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'
  15. dateext suffix '-2021-08-27-1485508280'
  16. 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]'
  17. compressing log with: /bin/gzip
  18. renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508280.log
  19. creating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4
  20. running postrotate script
  21. * Reloading nginx configuration nginx

第三次運(yùn)行它:

  1. reading config file /tmp/logrotate
  2. extension is now .log
  3.  
  4. Handling 1 logs
  5.  
  6. rotating pattern: /var/log/nginx/access.log
  7. /var/log/nginx/error.log   1 bytes (30 rotations)
  8. empty log files are not rotated, old logs are removed
  9. considering log /var/log/nginx/access.log
  10.   log needs rotating
  11. considering log /var/log/nginx/error.log
  12.   log does not need rotating
  13. rotating log /var/log/nginx/access.log, log->rotateCount is 30
  14. Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'
  15. dateext suffix '-2021-08-27-1485508316'
  16. 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]'
  17. compressing log with: /bin/gzip
  18. renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508316.log
  19. creating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4
  20. running postrotate script
  21. * Reloading nginx configuration nginx

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

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

 

 

責(zé)任編輯:龐桂玉 來(lái)源: Linux中國(guó)
相關(guān)推薦

2025-04-29 08:25:00

logrotateLinux日志輪轉(zhuǎn)

2021-08-16 11:59:32

Linuxlogrotate日志文件

2011-08-02 11:16:08

Oracle數(shù)據(jù)庫(kù)歸檔日志

2010-10-29 15:07:33

oracle日志

2010-10-29 14:44:35

ORACLE歸檔日志

2023-09-18 11:36:35

2009-12-08 12:10:30

2010-04-14 16:09:51

Oracle 10g歸

2010-11-19 13:19:26

Oracle歸檔日志

2010-11-19 13:14:21

Oracle刪除歸檔日

2013-11-06 13:31:14

Windowsrman備份日志

2010-10-29 13:30:33

Oracle歸檔日志

2021-07-20 05:33:41

Swift Codable

2021-02-19 18:06:57

Oracle日志聯(lián)機(jī)

2010-03-30 19:41:16

Nginx日志

2011-08-04 10:31:43

歸檔日志參數(shù)文件

2011-01-11 11:32:20

Linuxlogrotate配置

2023-11-28 08:52:48

Go日志庫(kù)

2010-09-02 15:42:37

echo命令

2020-05-22 15:45:30

Linuxlogrotate神器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)