Solaris中的日志轮换(压缩/删除)脚本

时间:2018-11-02 14:15:41

标签: bash unix solaris

在Linux环境中具有相同的日志轮换文件,并且在那里工作。在Solaris中,我在运行这些脚本时遇到了问题:

脚本的主要目的是删除所有30天以上的日志,并压缩所有5天以上的日志。使用-not -name是因为我只想对循环的日志文件进行操作,例如something.log.20181102,因为.log文件是当前文件,并且我不想触摸它们。

#!/bin/bash

find ./logs -mindepth 1 -mtime +30 -type f -not -name "*.log" -delete
find ./logs -mtime +5 -not -name "*.log" -exec gzip {} \;

-mindepth-not出现问题,因为它给出了错误:

find: bad option -not
find: [-H | -L] path-list predicate-list

基于搜索,我必须在搜索结果中以某种方式使用-prune,但我不太确定该如何使用。

2 个答案:

答案 0 :(得分:1)

如果您在Linux上查看find(1)的手册页(在Solaris上则查看gfind(1)的手册页),将会看到

-not expr
    Same as ! expr, but not POSIX compliant.

因此,尽管您需要使用反斜杠或引号将其从外壳中逸出,但您应该可以将-not替换为!

find ... \! -name "*.log" ...

请注意,在Solaris上,有一个名为logadm的命令旨在帮助您处理类似的事情,并且可能值得研究,除非您希望在Solaris和Linux上都具有完全相同的行为

答案 1 :(得分:0)

在@Danek Duvall的帮助下,通过一些搜索,我开始使用它了:

find ./logs -mtime +30 -type f ! -name "*.log" -exec rm -f {} \;
find ./logs -mtime +5 ! -name "*.log" -exec gzip {} \;

它将删除所有30天以上的日志文件,然后压缩5天以上的日志文件。

相关问题