AI_UIAutomation/scripts/clean-disk.sh

46 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# 定期清理测试生成物 + PyCharm freeze 转储,防止"系统数据"被打满。
# 由 crontab 每天调度(见文件末尾安装说明)。详见记忆 reference-midscene-disk-growth。
set -u
PROJ="/Users/woan/Desktop/AI_UIAutomation"
LOG="$PROJ/scripts/clean-disk.log"
{
echo "===== $(date '+%F %T') 开始清理 ====="
# 1. midscene 报告:纯生成产物,全清
if [ -d "$PROJ/midscene_run" ]; then
sz=$(du -sh "$PROJ/midscene_run" 2>/dev/null | cut -f1)
rm -rf "$PROJ/midscene_run"/* 2>/dev/null
echo "midscene_run 清理前 $sz → 0"
fi
# 2. reports:只保留"最近一次测试报告"(最新 AIHub_*.html 及其同一次运行的截图/日志)。
# 以第 2 新的报告为分界,比它旧的全删。
if [ -d "$PROJ/reports" ]; then
cutoff=$(ls -t "$PROJ/reports"/AIHub_*.html 2>/dev/null | sed -n '2p')
if [ -n "$cutoff" ] && [ -f "$cutoff" ]; then
n=$(find "$PROJ/reports" -type f ! -newer "$cutoff" 2>/dev/null | wc -l | tr -d ' ')
find "$PROJ/reports" -type f ! -newer "$cutoff" -delete 2>/dev/null
find "$PROJ/reports" -type d -empty -delete 2>/dev/null
echo "reports 只留最近一次,删除旧文件 $n"
else
echo "reports 报告数 <=1,不清理"
fi
fi
# 3. PyCharm freeze 转储:安全网(IDE 卡死会狂堆,实测堆到 23G)
dumps=$(ls -d ~/Library/Logs/JetBrains/PyCharm*/threadDumps-* 2>/dev/null | wc -l | tr -d ' ')
rm -rf ~/Library/Logs/JetBrains/PyCharm*/threadDumps-* 2>/dev/null
echo "PyCharm 转储目录清理 $dumps"
echo "可用空间: $(df -h / | tail -1 | awk '{print $4}')"
} >> "$LOG" 2>&1
# 只保留日志最后 200 行
tail -n 200 "$LOG" > "$LOG.tmp" 2>/dev/null && mv "$LOG.tmp" "$LOG"
# ---- 安装(每 2 周跑一次:每月 1 号、15 号 04:13)----
# chmod +x scripts/clean-disk.sh
# (crontab -l 2>/dev/null; echo "13 4 1,15 * * /Users/woan/Desktop/AI_UIAutomation/scripts/clean-disk.sh") | crontab -