Logrotate

日誌旋轉無法正常工作

  • January 6, 2020

我已經創建了日誌輪換,但它似乎對我不起作用,因為它正在創建大量文件,例如“smartfox.log.2020-01-05-07.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1 .1.1"

此外,rotate 3 和 maxsize 100M 也不起作用,因為我看到文件超過 3 天且超過 100M。

如果以下日誌輪換沒有錯誤,則應刪除 3 天以上的文件並僅保留 100M 日誌文件:

admin@ip-172-20-44-75:/etc/logrotate.d$ cat   /etc/logrotate.d/smartfox-qa
/var/log/cog-qa/smartfox-qa/*{

       rotate 3
       copytruncate
       missingok
       notifempty
       delaycompress
       maxsize 100M
       daily
       create 0644 root root
}

admin@ip-172-20-44-75:/var/log/cog-qa/smartfox-qa$ ls -lthr

-rw-r--r-- 1 root root    0 Jan  6 03:18 smartfox.log.2020-01-05-13.1.1.1.1.1.1.1.1.1.1.1.1.1
-rw-r--r-- 1 root root 655M Jan  6 03:18 smartfox.log.2020-01-05-14.1.1.1.1.1.1.1.1.1.1.1.1.1
-rw-r--r-- 1 root root    0 Jan  6 03:18 smartfox.log.2020-01-05-14.1.1.1.1.1.1.1.1.1.1.1.1
-rw-r--r-- 1 root root 656M Jan  6 03:18 smartfox.log.2020-01-05-15.1.1.1.1.1.1.1.1.1.1.1.1
-rw-r--r-- 1 root root    0 Jan  6 03:18 smartfox.log.2020-01-05-15.1.1.1.1.1.1.1.1.1.1.1
-rw-r--r-- 1 root root 653M Jan  6 03:18 smartfox.log.2020-01-05-16.1.1.1.1.1.1.1.1.1.1.1
-rw-r--r-- 1 root root    0 Jan  6 03:18 smartfox.log.2020-01-05-16.1.1.1.1.1.1.1.1.1.1
-rw-r--r-- 1 root root 661M Jan  6 03:18 smartfox.log.2020-01-05-17.1.1.1.1.1.1.1.1.1.1
-rw-r--r-- 1 root root    0 Jan  6 03:18 smartfox.log.2020-01-05-17.1.1.1.1.1.1.1.1.1
-rw-r--r-- 1 root root 660M Jan  6 03:19 smartfox.log.2020-01-05-18.1.1.1.1.1.1.1.1.1
-rw-r--r-- 1 root root    0 Jan  6 03:19 smartfox.log.2020-01-05-18.1.1.1.1.1.1.1.1
-rw-r--r-- 1 root root 665M Jan  6 03:19 smartfox.log.2020-01-05-19.1.1.1.1.1.1.1.1
-rw-r--r-- 1 root root    0 Jan  6 03:19 smartfox.log.2020-01-05-19.1.1.1.1.1.1.1
-rw-r--r-- 1 root root 658M Jan  6 03:19 smartfox.log.2020-01-05-20.1.1.1.1.1.1.1
-rw-r--r-- 1 root root    0 Jan  6 03:19 smartfox.log.2020-01-05-20.1.1.1.1.1.1
-rw-r--r-- 1 root root 675M Jan  6 03:19 smartfox.log.2020-01-05-21.1.1.1.1.1.1
-rw-r--r-- 1 root root    0 Jan  6 03:19 smartfox.log.2020-01-05-21.1.1.1.1.1
-rw-r--r-- 1 root root 656M Jan  6 03:19 smartfox.log.2020-01-05-22.1.1.1.1.1
-rw-r--r-- 1 root root    0 Jan  6 03:20 smartfox.log.2020-01-05-22.1.1.1.1
-rw-r--r-- 1 root root 660M Jan  6 03:20 smartfox.log.2020-01-05-23.1.1.1.1
-rw-r--r-- 1 root root    0 Jan  6 03:20 smartfox.log.2020-01-05-23.1.1.1
-rw-r--r-- 1 root root 649M Jan  6 03:20 smartfox.log.2020-01-06-00.1.1.1
-rw-r--r-- 1 root root    0 Jan  6 03:20 smartfox.log.2020-01-06-00.1.1
-rw-r--r-- 1 root root 662M Jan  6 03:20 smartfox.log.2020-01-06-01.1.1
-rw-r--r-- 1 root root    0 Jan  6 03:20 smartfox.log.2020-01-06-01.1
-rw-r--r-- 1 root root 661M Jan  6 03:20 smartfox.log.2020-01-06-02.1
-rw-r--r-- 1 root root    0 Jan  6 03:20 smartfox.log.2020-01-06-02

請讓我知道我錯在哪裡!

看來您不僅要輪換您可能想要的文件(smartfox.log.????-??-??-??),而且還要輪換 smartfox-qa 文件夾中的所有文件。如果是這樣,您應該更正您的萬用字元表達式

/var/log/cog-qa/smartfox-qa/smartfox.log.????-??-??-?? {
   rotate 3
   copytruncate
   missingok
   notifempty
   delaycompress
   maxsize 100M
   daily
   create 0644 root root
}

請注意 man logrotate 明確警告使用萬用字元

請謹慎使用萬用字元。如果您指定 *,logrotate 將旋轉所有文件,包括以前旋轉的文件。解決此問題的一種方法是使用 olddir 指令或更精確的萬用字元(例如 *.log)。

引用自:https://serverfault.com/questions/997734