Linux

logrotate 寫入舊的 app.log.1 而不是 app.log

  • February 28, 2017

我們有一個使用 syslog 進行日誌記錄的自定義應用程序,現在我們面臨 logrotate 的問題,即日誌正在寫入 urls.log.1 而不是 urls.log,logrotate 在輪換後沒有創建 url.log。我們正在使用Ubuntu 14.04 LTS 伺服器。我們如何強制 logrotate 在旋轉後創建 urls.log ?

日誌旋轉配置(/etc/logrotate.d/rsyslog)

/usr/local/app/log/urls.log
{
       rotate 10
       daily
       missingok
       notifempty
       compress
       delaycompress
       sharedscripts
       su app app
       postrotate
               reload rsyslog >/dev/null 2>&1 || true
       endscript
}

如果我手動 mv urls.log.1 到 urls.log 並執行 force logrotate ,這將是輸出

considering log /usr/local/app/log/urls.log
 log needs rotating
rotating log /usr/local/app/log/urls.log, log->rotateCount is 10
dateext suffix '-20170228'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
compressing log with: /bin/gzip
switching uid to 1000 and gid to 1000
renaming /usr/local/app/log/urls.log.10.gz to /usr/local/app/log/urls.log.11.gz (rotatecount 10, logstart 1, i 10), 
renaming /usr/local/app/log/urls.log.9.gz to /usr/local/app/log/urls.log.10.gz (rotatecount 10, logstart 1, i 9), 
renaming /usr/local/app/log/urls.log.8.gz to /usr/local/app/log/urls.log.9.gz (rotatecount 10, logstart 1, i 8), 
renaming /usr/local/app/log/urls.log.7.gz to /usr/local/app/log/urls.log.8.gz (rotatecount 10, logstart 1, i 7), 
renaming /usr/local/app/log/urls.log.6.gz to /usr/local/app/log/urls.log.7.gz (rotatecount 10, logstart 1, i 6), 
renaming /usr/local/app/log/urls.log.5.gz to /usr/local/app/log/urls.log.6.gz (rotatecount 10, logstart 1, i 5), 
renaming /usr/local/app/log/urls.log.4.gz to /usr/local/app/log/urls.log.5.gz (rotatecount 10, logstart 1, i 4), 
renaming /usr/local/app/log/urls.log.3.gz to /usr/local/app/log/urls.log.4.gz (rotatecount 10, logstart 1, i 3), 
renaming /usr/local/app/log/urls.log.2.gz to /usr/local/app/log/urls.log.3.gz (rotatecount 10, logstart 1, i 2), 
renaming /usr/local/app/log/urls.log.1.gz to /usr/local/app/log/urls.log.2.gz (rotatecount 10, logstart 1, i 1), 
renaming /usr/local/app/log/urls.log.0.gz to /usr/local/app/log/urls.log.1.gz (rotatecount 10, logstart 1, i 0), 
old log /usr/local/app/log/urls.log.0.gz does not exist
renaming /usr/local/app/log/urls.log to /usr/local/app/log/urls.log.1
running postrotate script
switching euid to 0 and egid to 0
removing old log /usr/local/app/log/urls.log.11.gz
switching euid to 0 and egid to 0

如果我再次強制執行 logrotate 這將是輸出

   rotating pattern: /usr/local/app/log/urls.log 
forced from command line (100 rotations)
empty log files are not rotated, old logs are removed
switching euid to 1000 and egid to 1000
considering log /usr/local/app/log/urls.log  
 log /usr/local/app/log/urls.log does not exist -- skipping  ## urls.log is not creating
not running postrotate script, since no logs were rotated
switching euid to 0 and egid to 0

您可以使用以下copytruncate選項:

copytruncate

創建副本後將原始日誌文件截斷到位,而不是移動舊日誌文件並選擇性地創建新日誌文件,當某些程序無法被告知關閉其日誌文件並因此可能繼續寫入(附加)到時,可以使用它以前的日誌文件永遠。請注意,複製文件和截斷文件之間的時間片非常短,因此可能會失去一些日誌記錄數據。

$$ … $$

使用此選項,您可能不必使用該postrotate位。

引用自:https://unix.stackexchange.com/questions/348202