补充说明
用 crontab 定时清理临时目录,按「最后访问时间」把长时间无人访问的文件和子目录删掉,适合放在 NAS / 路由 / 服务器上做 /tmp、下载中转目录、上传缓存目录的自动回收。
核心任务行:
*/10 * * * * find /vol2/1000/tmp -mindepth 1 -atime +0.5 -delete
含义:每 10 分钟扫一次 /vol2/1000/tmp,把 12 小时以上没有被访问过的内容删掉,目录本身保留。
⚠️ 这条命令写法简洁,但有几个容易踩坑的行为特性(目录不会被递归清空、relatime 时间精度、空目录残留等),下文「行为实测」与「风险提示」章节均有真实环境验证,建议先读完再投用。
参数解析
| 参数 | 含义 |
|---|---|
*/10 * * * * |
crontab 时间字段,分 时 日 月 周,*/10 表示每 10 分钟执行一次 |
/vol2/1000/tmp |
查找的起始目录(扫描根) |
-mindepth 1 |
从第 1 层开始处理,即不把 /vol2/1000/tmp 本身纳入匹配,根目录永远不会被删 |
-atime +0.5 |
匹配「最后访问时间早于 0.5 天(12 小时)」的条目 |
-delete |
删除匹配到的文件;目录只有在清空之后为空时才会被删除 |
-mindepth 1
find 默认从第 0 层(起始目录自身)开始匹配。若不加该参数,且 /vol2/1000/tmp 本身的 atime 也超过 12 小时,理论上它可能被 -delete 处理掉。加了 -mindepth 1 后,起始目录被排除在删除范围之外,只清理里面的内容(文件 + 子目录)。
-atime +0.5
atime= access time,最后一次被读取 / 被访问的时间;find的时间参数单位是「天」,+0.5表示 0.5 天 = 12 小时;+n是「大于 n」的意思,即最后访问时间早于 12 小时前才匹配;-0.5则表示 12 小时之内访问过(不匹配)。
# 12 小时以上没访问过 → 匹配(会被删)
find /vol2/1000/tmp -mindepth 1 -atime +0.5 -print
# 24 小时以上没访问过 → 匹配
find /vol2/1000/tmp -mindepth 1 -atime +1 -print
# 60 分钟以上没访问过
find /vol2/1000/tmp -mindepth 1 -amin +60 -print
-delete
-delete 属于 find 的「操作(Action)」,等价于 -depth(后序遍历)+ 删除动作,有两点必须明确:
- 删除目录的前提是该目录已经为空;
-delete隐含-depth,因此find会先处理目录里的内容、再处理目录本身,目录里残留的不匹配文件会阻止目录被删除。
行为实测
实测一:-delete 不会「递归清空目录」
常见的误解是「-delete 删目录时会把目录里所有内容一起清掉」。实测并非如此:
# 准备:d1 内含一个「不匹配」的近期文件 keep.txt,d1 本身 atime 设为 3 天前
find /tmp/t -mindepth 1 -atime +0.5 -delete
# 输出:find: cannot delete '/tmp/t/d1': Directory not empty
# 退出码:1
# 结果:d1 及其内容原样保留
只有当目录自身匹配 atime 且内部内容已被删空时,目录才会被删除:
# 准备:d1/old.txt(atime 3 天前)、d1/sub/deep.txt(atime 3 天前)、new.txt(近期)
find /tmp/t -mindepth 1 -atime +0.5 -delete
# 结果:old.txt、d1/sub/deep.txt、d1、d1/sub 全部删除,new.txt 保留
实测二:目录 atime 会被 find 自己刷新 → 空目录长期残留
find 读取目录项时会更新目录的 atime(relatime 下当 atime 早于 ctime 或超过 24 小时即刷新)。实测过程:
# 第一次执行:d 的 atime 为 3 天前,内部有近期文件 → 匹配 atime,但目录非空
find /tmp/t -mindepth 1 -atime +0.5 -delete
# find: cannot delete '/tmp/t/d': Directory not empty 退出码 1
# 此时 d 的 atime 已被本次 readdir 刷新为「当前时间」
# 第二次执行:d 的 atime 已是刚才,不再匹配 → 直接跳过,目录永久残留
find /tmp/t -mindepth 1 -atime +0.5 -delete
# 无输出,退出码 0,d 依旧存在(空壳目录)
结论:目录被清空后通常不会被同一条命令删掉,因为它的 atime 刚被 find 自己刷新过。需要单独用「空目录清理」规则兜底。
实测三:relatime 的 24 小时粒度会让 atime 判定偏激进
Linux 默认挂载 relatime(可 mount | grep / 确认),atime 的刷新规则是:上一次 atime 早于 mtime 或 ctime,或上一次 atime 超过 24 小时。实测:
# 连续两次读取同一个文件
cat f.txt >/dev/null # read1 atime: 07:56:28
cat f.txt >/dev/null # read2 atime: 07:56:28 ← 未刷新
也就是说 24 小时内的重复访问不会更新 atime。配合 -atime +0.5(阈值仅 12 小时)会出现误删:某个 1 小时前刚被访问过、但 atime 记录停留在 20 小时前的文件,仍会被判定为「12 小时未访问」而遭删除。
实测四:noatime 挂载下 atime 不可用
若分区以 noatime 挂载,atime 永远停留在「创建时间」,-atime 条件会退化成「按创建时间判断」,任何超过 12 小时的文件都会被删,与实际是否被使用无关。
风险提示
| 风险点 | 说明 | 规避建议 |
|---|---|---|
-delete 不递归清空目录 |
目录非空时会报 Directory not empty 并以退出码 1 结束 |
拆成「删文件」+「删空目录」两条规则 |
| 目录 atime 被 find 刷新 | 目录变空后不再匹配,残留空壳目录 | 追加 -type d -empty -delete 兜底 |
| relatime 24h 粒度 | 12 小时阈值过于激进,可能删掉仍在使用的文件 | 改用 -mtime(写入时间)判断,或把阈值放大到 1 天 |
| 写入中的文件被删 | 写入只更新 mtime,不更新 atime,正在下载/上传的文件可能被删 | 用 -mmin / -mtime 判断 |
| noatime 挂载 | atime 不更新,判定完全失真 | 检查挂载参数,改用 mtime |
| find 报错触发告警 | cron 中命令非 0 退出会向 MAILTO 发邮件,10 分钟一封 |
加 2>&1 | logger 或重定向到日志 |
| 路径写错 | 路径写错等于批量删除错误目录 | 上线前先用 -print 干跑确认 |
配置步骤
1. 先干跑(dry-run)确认删除范围
把 -delete 换成 -print,只看不删:
find /vol2/1000/tmp -mindepth 1 -atime +0.5 -print
确认输出符合预期后再进入下一步。
2. 编辑 crontab
crontab -e
写入(推荐带日志、且区分文件与空目录):
# 每 10 分钟清理 /vol2/1000/tmp 中 12 小时以上未访问的内容
*/10 * * * * find /vol2/1000/tmp -mindepth 1 -type f -atime +0.5 -delete 2>&1 | logger -t crontab-tmp
# 清理残留的空目录
*/10 * * * * find /vol2/1000/tmp -mindepth 1 -type d -empty -delete 2>&1 | logger -t crontab-tmp
需要严格按原始需求(不加
-type f,让目录也参与 atime 判定)时,直接使用:
*/10 * * * * find /vol2/1000/tmp -mindepth 1 -atime +0.5 -delete
3. 验证任务已写入
crontab -l
输出应包含:
*/10 * * * * find /vol2/1000/tmp -mindepth 1 -atime +0.5 -delete
4. 手动执行一次验证效果
# 直接跑一次,观察会不会报错
find /vol2/1000/tmp -mindepth 1 -atime +0.5 -delete; echo "exit=$?"
# 统计当前会删除多少条目
find /vol2/1000/tmp -mindepth 1 -atime +0.5 -print | wc -l
5. 查看执行日志
# journalctl 方式
journalctl -t crontab-tmp -n 50 --no-pager
# 或者直接写入日志文件
*/10 * * * * find /vol2/1000/tmp -mindepth 1 -type f -atime +0.5 -delete >> /var/log/tmp_clean.log 2>&1
tail -f /var/log/tmp_clean.log
6. 配置日志轮转(logrotate)
清理任务每 10 分钟写一次日志,不做轮转会持续膨胀,用 logrotate 限制保留份数:
cat > /etc/logrotate.d/crontab_tmp <<'EOF'
/var/log/crontab_tmp.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
copytruncate
}
EOF
# 校验配置(-d 为模拟运行,不会实际轮转)
logrotate -d /etc/logrotate.d/crontab_tmp
| 配置项 | 作用 |
|---|---|
daily |
按天轮转 |
rotate 7 |
保留最近 7 份历史日志 |
compress / delaycompress |
压缩归档,延迟一轮再压缩(避免与写入冲突) |
missingok |
日志文件不存在时不报错 |
notifempty |
空日志不轮转 |
copytruncate |
复制后清空原文件,脚本可继续按路径追加写入 |
一键安装(推荐)
上面 5 步手动配置比较繁琐,仓库提供了配套的一键安装脚本,一条命令完成「清理脚本 + 计划任务 + 日志轮转」全部部署:
sudo bash <(curl -sSL gitee.com/meimolihan/cmdbox/raw/master/sh/install_crontab_tmp.sh)
或从本地下载后执行:
curl -fsSL https://gitee.com/meimolihan/cmdbox/raw/master/sh/install_crontab_tmp.sh -o /tmp/install_crontab_tmp.sh
sudo bash /tmp/install_crontab_tmp.sh
安装器做了什么:
| 步骤 | 动作 | 产物 |
|---|---|---|
| 1 | 生成清理脚本(权限 700) | /usr/local/bin/crontab_tmp.sh |
| 2 | 备份原 crontab → 去重旧条目 → 追加新任务 → 校验 | crontab 中一条 */10 * * * * 任务 |
| 3 | 生成并校验 logrotate 配置 | /etc/logrotate.d/crontab_tmp(按天轮转,保留 7 份) |
| 4 | 询问是否立即执行一次清理验证 | /var/log/crontab_tmp.log 日志 |
安装器幂等,可重复执行:旧的 crontab_tmp.sh 任务行会被自动移除后重新追加,不会出现重复任务。
脚本源码与详细说明见 install_crontab_tmp。
安装后的常用命令
tail -f /var/log/crontab_tmp.log # 查看日志
bash /usr/local/bin/crontab_tmp.sh # 手动立即清理
find /vol2/1000/tmp -type f -atime +0.5 # 预览将被删的文件(不含 -delete)
crontab -l # 查看计划任务
清理脚本做了什么
# 1) 删除过期文件(先记录清单再删除)
find "$TMP_DIR" -mindepth 1 -type f -atime "$EXPIRE_ATIME" -print -delete
# 2) 删除空目录
find "$TMP_DIR" -mindepth 1 -type d -empty -print -delete
两条 find 的
-mindepth 1不能省:-type d -empty -delete若缺少它,临时目录被清空后会连目录本身一起删掉。实测确认:
$ mkdir -p /tmp/bug1/tmp && find /tmp/bug1/tmp -type d -empty -delete
$ ls -d /tmp/bug1/tmp
ls: cannot access '/tmp/bug1/tmp': No such file or directory # 目录被误删
卸载
crontab -l | grep -vF "/usr/local/bin/crontab_tmp.sh" | crontab -
rm -f /usr/local/bin/crontab_tmp.sh /etc/logrotate.d/crontab_tmp /var/log/crontab_tmp.log
推荐写法对比
| 场景 | 推荐命令 |
|---|---|
| 只想删「很久没人碰」的文件 | find /vol2/1000/tmp -mindepth 1 -type f -mtime +1 -delete |
| 想删 12 小时未访问的文件 | find /vol2/1000/tmp -mindepth 1 -type f -atime +0.5 -delete |
| 文件 + 空目录一起清 | find /vol2/1000/tmp -mindepth 1 -type f -mtime +1 -delete + find /vol2/1000/tmp -mindepth 1 -type d -empty -delete |
| 只按访问时间、不区分类型(原需求) | find /vol2/1000/tmp -mindepth 1 -atime +0.5 -delete |
| 需要排除某些子目录 | find /vol2/1000/tmp -mindepth 1 -path '/vol2/1000/tmp/keep' -prune -o -type f -mtime +1 -delete |
-mtime/-mmin依据「内容最后修改时间」,写入中的文件会持续刷新 mtime,不会被误删;相比atime更适合临时目录场景。
故障排查
任务没有执行
# 查看 cron 服务状态
systemctl status cron
systemctl status crond
# 启动并设置开机自启
systemctl enable --now cron
目录没有被清空(只剩空壳)
这是 -delete 的正常行为:目录自身的 atime 会被 find 读取时刷新,导致下一轮不再匹配。用空目录清理规则兜底:
find /vol2/1000/tmp -mindepth 1 -type d -empty -delete
报 Directory not empty
说明目录自身匹配了时间条件,但内部还有不匹配的内容。改用 -type f 精确删除文件,再单独清理空目录。
明明刚访问过的文件还是被删了
检查挂载参数与 relatime 精度:
mount | grep -E " / |/vol2"
# noatime → atime 不可用,必须改用 mtime
# relatime → atime 24 小时粒度,阈值建议改为 -atime +1(24 小时)
收到大量 cron 告警邮件
命令非 0 退出会触发邮件通知。三种处理方式:
# 1) 重定向到日志文件
*/10 * * * * find /vol2/1000/tmp -mindepth 1 -atime +0.5 -delete >> /var/log/tmp_clean.log 2>&1
# 2) 交给 syslog
*/10 * * * * find /vol2/1000/tmp -mindepth 1 -atime +0.5 -delete 2>&1 | logger -t crontab-tmp
# 3) 直接关闭邮件(/etc/crontab 中设置)
# MAILTO=""
确认当前已删除 / 待删除的内容
# 待删除清单
find /vol2/1000/tmp -mindepth 1 -atime +0.5 -print
# 目录占用情况
du -sh /vol2/1000/tmp
常用配套命令
# 临时查看目录内容与访问时间
ls -l --time=atime /vol2/1000/tmp
ls -lu /vol2/1000/tmp
# 统计各子目录占用
du -sh /vol2/1000/tmp/* 2>/dev/null | sort -h
# 一次性清空(不考虑时间,慎用)
find /vol2/1000/tmp -mindepth 1 -delete