温馨提示×

如何验证linux cleanup

小樊
35
2026-09-29 13:23:43
栏目: 智能运维

在 Linux 里,“cleanup(清理)”通常指系统资源、进程、临时文件、磁盘、容器/服务等的清理操作。验证 cleanup 是否“真的生效”,一般从结果检查 + 日志 + 监控三方面入手。下面按常见场景给出验证方法。


一、验证临时文件 / 磁盘清理

1. 检查磁盘空间

df -h

对比清理前后:

df -h /tmp

2. 检查目录内容

ls -lh /tmp
du -sh /var/log/*

3. 验证 systemd 临时文件清理

systemd-tmpfiles --clean

检查:

systemctl status systemd-tmpfiles-clean

二、验证进程 / 服务 cleanup

1. 进程是否还在

ps aux | grep <process>
pgrep -a <process>

2. 端口是否释放

ss -lntp | grep <port>
netstat -lntp

3. systemd 服务

systemctl stop myservice
systemctl is-active myservice

应为:

inactive

三、验证内存 / 缓存清理

1. 清理前

free -h

2. 清理缓存(示例)

sync
echo 3 > /proc/sys/vm/drop_caches

3. 验证

free -h

观察 buff/cache 是否下降。


四、验证日志 cleanup

1. journald

journalctl --disk-usage

清理:

journalctl --vacuum-size=100M

再次:

journalctl --disk-usage

2. logrotate 验证

logrotate -d /etc/logrotate.conf

(-d 是 dry-run,不实际删除)


五、验证容器 / Docker cleanup

1. 容器

docker ps -a
docker container prune
docker ps -a

2. 镜像 / 卷

docker images
docker volume ls
docker system df

六、验证脚本化 cleanup

如果是你写的 cleanup 脚本:

建议做法

  • 清理前记录状态
  • 清理后对比
  • 输出日志

示例:

before=$(df -h / | awk 'NR==2{print $5}')
./cleanup.sh
after=$(df -h / | awk 'NR==2{print $5}')
echo "Before: $before After: $after"

七、通用验证原则(重点)

✅ 看资源是否减少
✅ 看进程/端口是否消失
✅ 看日志是否记录成功
✅ 用前后对比而不是“以为清了”


如果你指的是某一类 cleanup(比如:kernel cleanup、Kubernetes cleanup、CI/CD cleanup、自写脚本),可以直接说场景,我可以给你更精确的验证命令。

0 踩