如果我有这样的 logrotate 配置文件,
# matches multiple ones
/var/log/project/*.log {
...
prerotate
...
endscript
...
}
那么 glob 在这里是如何工作的呢?如果我有 3 个日志文件与该模式匹配,预旋转脚本会执行 3 次还是只执行一次?我没有找到任何线索logrotate (8)
答案1
就其价值而言,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
...
}
答案2
它执行三次,每个匹配文件执行一次。手册页中有一个提示:
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
;))