You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 copytruncate parameter 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 .gz ones. Logrotate tries to rotate these again, leading to the extra .1.gz suffix.

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, so rotate 30 is sufficient.
  • Removed copytruncate: Since these xxx.log.YYYY-MM-DD files 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 prerotate script: 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), and nosharedscripts (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:32:47