logrotate prerotate 脚本没有获取要旋转的文件的名称

logrotate prerotate 脚本没有获取要旋转的文件的名称

我在使用 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
}

这是/root/filter-syslog.sh

#!/bin/sh 
echo "log file subject to prerotate: $1"

最后,以下是 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

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

答案1

您没有将任何内容传递给 shell 脚本。因此,$1您的脚本中/root/filter-syslog.sh没有任何值,因此不会打印任何内容。

echo "log file subject to prerotate: $1"

为了使其工作,您必须在 logrotate 配置中传递$1或。$@

compress
/external-logs/syslog {
  daily
  rotate 9
  olddir OLD
  missingok
  prerotate
    /root/filter-syslog.sh $1
  endscript
}

相关内容