我在使用 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
不是“”?
您没有将任何内容传递给您的 shell 脚本。所以
$1
in your/root/filter-syslog.sh
没有任何价值,因此没有打印任何内容。要使其工作,您必须通过
$1
或$@
在您的 logrotate 配置中。