Logrotate

logrotate prerotate 腳本未獲取要旋轉的文件名

  • March 9, 2019

我在使用 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 腳本。所以$1in your/root/filter-syslog.sh沒有任何價值,因此沒有列印任何內容。

echo "log file subject to prerotate: $1"

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

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

引用自:https://serverfault.com/questions/875235