2017-09-01 55 views

回答

1

在我看來,你可以使用基於年齡刪除文件的標準方法,稍作修改以降低文件系統過滿時的閾值。

刪除所有*.thumb文件/tmp超過一定年齡(約一個月)的通常方法是用以下命令:

find /tmp -type f -name '*.thumb' -mtime +30 -delete 

所以,你需要做的是降低門檻是在某些情況下修改mtime測試。要做到這一點基於如何充分的文件系統可能會喜歡的東西來完成:

#!/usr/bin/env bash 

# Default to about a month. 

thresh=30 

# Get percentage used of /tmp, needs to match output of df, such as: 
# Filesystem  1K-blocks  Used Available Use% Mounted on 
# tmp    1000000 280000 720000 28% /tmp 

tmppct=$(df | awk '$6=="/tmp" { gsub("%", "", $5); print $5 }') 

# Reduce threshold if tmp more than 80% full. 

[[ ${tmppct} -gt 80 ]] && thresh=1 

# Go and clean up, based on threshold. 

find /tmp -type f -name '*.thumb' -mtime +${thresh} -delete 

的只是腳本傳遞的df(根據指定的格式)的輸出通過的可能有點棘手:

awk '$6=="/tmp" { gsub("%", "", $5); print $5 }' 

這只是將:

  • 找其中第六字段是/tmp線;
  • 從第五個字段中刪除尾部%;和
  • 最終輸出(修改)的第五個字段來捕獲完整的百分比。

然後,只需創建一個crontab條目,該條目將定期運行該腳本。

相關問題