[英]Log rotation (zipping/deleting) script in 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
,但是我不太確定該如何使用。
如果查看Linux上的find(1)
的手冊頁(或Solaris上的gfind(1)
),則會看到
-not expr
Same as ! expr, but not POSIX compliant.
因此,您應該可以將-not
替換為!
,盡管您需要使用反斜杠或單引號將其從外殼中逸出:
find ... \! -name "*.log" ...
請注意,在Solaris上,有一個名為logadm
的命令旨在幫助您處理類似的事情,並且可能值得研究,除非您希望在Solaris和Linux上具有完全相同的行為。
在@Danek Duvall的幫助下,通過一些搜索,我開始使用它:
find ./logs -mtime +30 -type f ! -name "*.log" -exec rm -f {} \;
find ./logs -mtime +5 ! -name "*.log" -exec gzip {} \;
它將刪除所有超過30天的日志文件,然后壓縮超過5天的日志文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.