Logrotate

logrotate 如何處理 globbing?

  • February 16, 2016

如果我有這樣的 logrotate 配置文件,

# matches multiple ones
/var/log/project/*.log {
  ...

  prerotate
     ...
  endscript

  ...
} 

那麼 glob 在這裡是如何工作的呢?如果我有 3 個日誌文件與該模式匹配,prerotate 腳本會執行 3 次還是只執行一次?我沒有找到任何線索logrotate (8)

它執行了 3 次,每個匹配文件執行一次。手冊頁中有一個提示:

sharedscripts
      Normally,  prerotate  and postrotate scripts are run for each log which is rotated and the absolute path
      to the log file is passed as first argument to the script. That means a single script may be run  multi-
      ple  times  for  log  file  entries which match multiple files (such as the /var/log/news/* example). If
      sharedscripts is specified, the scripts are only run once, no matter how many logs match the  wildcarded
      pattern,  and  whole  pattern  is  passed  to them.  However, if none of the logs in the pattern require
      rotating, the scripts will not be run at all. If the scripts exit with error, the remaining actions will
      not  be  executed  for  any  logs.  This  option overrides the nosharedscripts option and implies create
      option.

但是,當然,您只有在知道要查看那裡時才會發現。(另外,我用 ;) 進行了實驗驗證logrotate -v

對於它的價值,logrotate 使用 glob.h(參見:)man 3 glob,它在man 7 glob. 它在許多方面與 bash 萬用字元(沒有擴展萬用字元)相似,但並不完全相同。特別是,這意味著它支持:

?      match a single character
*      match any string, including the empty string
[...]  match any of the listed characters
[a-z]  match a range of characters
[!...] match any but the listed characters
[:xx:] match various character classes, like [:digit:], [:blank:], etc.

萬用字元分別應用於路徑的每個組件。例如,如果我有一個 rsyslog 伺服器從多個主機收集日誌,我可以使用如下 logrotate 節:

/var/log/host/*/*/syslog {
   rotate 5
   ...
}

引用自:https://unix.stackexchange.com/questions/67860