logrotate 如何处理通配符?

dai*_*isy 5 logrotate glob

如果我有这样的 logrotate 配置文件,

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

   prerotate
      ...
   endscript

   ...
} 
Run Code Online (Sandbox Code Playgroud)

那么 glob 在这里是如何工作的呢?如果我有 3 个与该模式匹配的日志文件,预旋转脚本会执行 3 次还是仅执行一次?我没有找到任何线索logrotate (8)

小智 7

对于它的价值,logrotate 使用 glob.h(参见:)man 3 glob,它在man 7 glob. 它在很多方面都类似于 bash globbing(没有扩展的 globbing),但并不完全相同。特别是,这意味着它支持:

?      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.
Run Code Online (Sandbox Code Playgroud)

Globbing 分别应用于路径的每个组件。例如,如果我有一个 rsyslog 服务器从多个主机收集日志,我可以使用 logrotate 节,如:

/var/log/host/*/*/syslog {
    rotate 5
    ...
}
Run Code Online (Sandbox Code Playgroud)


Ulr*_*arz 4

它执行三次,每个匹配文件执行一次。手册页中有一个提示:

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.
Run Code Online (Sandbox Code Playgroud)

但当然,只有当你知道去那里看时,你才会发现这一点。(另外,我用实验验证了logrotate -v;))