logrotate prerotate 脚本未获取要旋转的文件名

use*_*042 2 logrotate

我在使用 logrotate 3.12.2 中的 prerotate 脚本时遇到了问题。我在该prerotate/endscript部分中指定的脚本没有接收要轮换的文件名作为第一个参数 ( $1)。

这是 logrotate 配置:

compress
/external-logs/syslog {
  daily
  rotate 3
  olddir OLD
  missingok
  prerotate
    /root/filter-syslog.sh
  endscript
}
Run Code Online (Sandbox Code Playgroud)

这是/root/filter-syslog.sh

#!/bin/sh 
echo "log file subject to prerotate: $1"
Run Code Online (Sandbox Code Playgroud)

最后,这是 logrotate 运行的详细输出:

reading config file /external-config/logrotate.conf
olddir is now OLD
Reading state from file: /external-logs/logrotate.state
Allocating hash table for state file, size 64 entries
Creating new state

Handling 1 logs

rotating pattern: /external-logs/syslog  after 1 days (3 rotations)
olddir is OLD, empty log files are rotated, old logs are removed
considering log /external-logs/syslog
  Now: 2017-09-24 13:35
  Last rotated at 2017-09-23 12:50
  log needs rotating
rotating log /external-logs/syslog, log->rotateCount is 3
dateext suffix '-20170924'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /external-logs/OLD/syslog.3.gz to /external-logs/OLD/syslog.4.gz (rotatecount 3, logstart 1, i 3), 
old log /external-logs/OLD/syslog.3.gz does not exist
renaming /external-logs/OLD/syslog.2.gz to /external-logs/OLD/syslog.3.gz (rotatecount 3, logstart 1, i 2), 
old log /external-logs/OLD/syslog.2.gz does not exist
renaming /external-logs/OLD/syslog.1.gz to /external-logs/OLD/syslog.2.gz (rotatecount 3, logstart 1, i 1), 
old log /external-logs/OLD/syslog.1.gz does not exist
renaming /external-logs/OLD/syslog.0.gz to /external-logs/OLD/syslog.1.gz (rotatecount 3, logstart 1, i 0), 
old log /external-logs/OLD/syslog.0.gz does not exist
log /external-logs/OLD/syslog.4.gz doesn't exist -- won't try to dispose of it
running prerotate script
log file subject to prerotate: 
renaming /external-logs/syslog to /external-logs/OLD/syslog.1
compressing log with: /bin/gzip
Run Code Online (Sandbox Code Playgroud)

注意log file subject to prerotate:上面输出中的“ ”行。为什么不是行输出“ log file subject to prerotate: /external-logs/syslog”?

Tho*_*mas 5

您没有将任何内容传递给您的 shell 脚本。所以$1in your/root/filter-syslog.sh没有价值,因此没有打印任何内容。

echo "log file subject to prerotate: $1"
Run Code Online (Sandbox Code Playgroud)

要使其工作,您必须通过$1$@在您的 logrotate 配置中。

compress
/external-logs/syslog {
  daily
  rotate 9
  olddir OLD
  missingok
  prerotate
    /root/filter-syslog.sh $1
  endscript
}
Run Code Online (Sandbox Code Playgroud)