logrotate异常日志轮转求助:仅轮转日期命名日志不触及主日志
Hey there, let's work through your logrotate issue step by step!
First, let's break down why your current config is causing those two problems:
- Uncompressed final logs: The
copytruncateparameter is meant for actively written log files (to truncate the original after copying), but your target files are already archived and no longer being written to. This parameter is interfering with the compression process here. - .1.gz suffix on compressed files: Your config is matching all
xxx.log.*files—including already compressed.gzones. Logrotate tries to rotate these again, leading to the extra.1.gzsuffix.
Here's a fixed logrotate config tailored to your needs:
/var/log/xxx/xxx/xxx.log.* { missingok rotate 30 compress notifempty nosharedscripts prerotate # Skip already compressed files to avoid double-processing if [[ "$1" == *.gz ]]; then exit 1 fi endscript }
Let's go over the key changes:
- Removed
daily: This parameter is for rotating active logs on a daily schedule, but your target files are already daily archived. We just need to retain the last 30 of them, sorotate 30is sufficient. - Removed
copytruncate: Since thesexxx.log.YYYY-MM-DDfiles are no longer being written to by your app, there's no need to copy and truncate them. Removing this lets compression work as expected. - Fixed the
prerotatescript: The original regex was incorrect. Now we simply check if the file ends with.gz—if so, we exit with code 1, which tells logrotate to skip processing that file entirely. This prevents the double-compression issue. - Kept the parameters that make sense for your use case:
missingok(no error if some files are missing),compress(compress the archived logs),notifempty(ignore empty files), andnosharedscripts(run the prerotate script for each file individually).
With this config, logrotate will only process your dated xxx.log.YYYY-MM-DD files (skipping any already compressed ones), compress them, keep the last 30, and leave your active xxx.log completely untouched.
内容的提问来源于stack exchange,提问作者bitUp
相关产品推荐
相关产品推荐

