我可以使用logrotate来旋转/核心文件?

在远程基于Linux的设备上,我想我会使用logrotate来pipe理我们的设备可能创build的任何核心文件。 但似乎logrotate认为每个核心文件是一个独特的文件,因为文件名包含PID。 这打破了logrotate正常旋转文件的方式。 例如:

core_123 core_222 core_555 

而不是将这些视为同一个文件的3个变体,而是将其视为3个独特的文件。 因此,如果我在/etc/logrotate.d/core rotate 50 ,它将会愿意旋转50个不同的core_123文件和50个不同的core_222文件等,从而导致潜在的数百或数千个文件。 相反,我想确保logrotatepipe理最多50个core_ *文件。

这是我试图做的确切的logrotate文件:

 /mycores/core_* { compress daily maxage 28 missingok nocreate nodelaycompress olddir /mycores/old rotate 50 } 

我怀疑这是不可能与logrotate,但我想我会张贴在serverfault,以防万一我错过了在文档中的东西。

我们使用这样的脚本。 我没有看到涉及logrotate的点

  1. 这些不是日志文件
  2. 你不旋转他们

脚本:

 #!/bin/bash EMAIL_TO="<%= mail %>" EMAIL_SUBJ="Found core files on `hostname -s`" DATETIME=`date +"%F-%H%M%S"` EMPTY_CORE_FILES=`find /var/log/core/ -mmin +60 -type f -empty -not -name "*.gz" -not -name "*.zip" -print -exec rm {} \;` CORE_FILES=`find /var/log/core/ -mmin +60 -type f -not -empty -not -name "*.gz" -not -name "*.zip"` NUM_CORE_FILES=`echo $CORE_FILES | wc -w` lockfile="/tmp/`basename $0`.lock" lockfile -1 -r 5 -l 84600 ${lockfile} || exit 1 trap "/bin/rm -f $lockfile" EXIT INT TERM for f in $CORE_FILES; do gzip -9 $f done if [ $NUM_CORE_FILES != 0 ] then mail "$EMAIL_TO" -s "$EMAIL_SUBJ" <<EOF I found and gzipped $NUM_CORE_FILES on $HOSTNAME in /var/log/core, please investigate. Files Found: $CORE_FILES Empty Files: $EMPTY_CORE_FILES EOF fi 

我将logrotate和一些shell魔法结合起来,以达到理想的效果。 基本上,logrotate仍然负责移动和压缩文件,然后开始删除旧文件所需的脚本以及确保目录中没有超过50个文件。 这是我做的:

 /mycores/core_* { compress daily missingok nocreate nodelaycompress olddir /mycores/old sharedscripts postrotate # delete all core_*.gz files older than 28 days find /mycores/old -name "core_*.gz" -mtime +28 -delete # make sure we have a max of 50 files; delete the oldest files if we have too many for filename in $(find /mycores/old/ -name "core_*.gz" -printf "%T+ %p\n" | sort --reverse | tail --lines=+51 | cut -d' ' -f2); do rm $filename; sleep 0.5; done endscript } 

我不是那里最好的bash scripter,所以postrotate脚本部分可能比必要的更重… 🙂