可以的,CentOS 的 cleanup(清理)完全可以自动化。常见做法有 shell 脚本 + cron、systemd timer,也可以结合监控/日志工具。下面按“能自动做什么 → 怎么做 → 示例”来说明。
yum clean all
# CentOS 8+/Stream 也可用
dnf clean all
CentOS 默认会保留多个内核,时间久了 /boot 可能满。
package-cleanup --oldkernels --count=2
journalctl --vacuum-size=500M.log、.gzrm -rf /tmp/*
rm -rf /var/tmp/*
如:
docker system prune -f/usr/local/bin/centos-cleanup.sh
#!/bin/bash
# CentOS 自动清理脚本
# 清理 yum/dnf 缓存
yum clean all >/dev/null 2>&1
# 保留最近2个内核
package-cleanup --oldkernels --count=2 -y >/dev/null 2>&1
# 清理 journal 日志
journalctl --vacuum-size=500M >/dev/null 2>&1
# 清理临时文件
rm -rf /tmp/* /var/tmp/*
# 清理 30 天前的日志
find /var/log -type f -name "*.log" -mtime +30 -delete
赋权:
chmod +x /usr/local/bin/centos-cleanup.sh
crontab -e
0 3 * * 0 /usr/local/bin/centos-cleanup.sh
/etc/systemd/system/centos-cleanup.service
[Unit]
Description=CentOS Cleanup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/centos-cleanup.sh
/etc/systemd/system/centos-cleanup.timer
[Unit]
Description=Run CentOS cleanup weekly
[Timer]
OnCalendar=weekly
Persistent=true
[Install]
WantedBy=timers.target
systemctl enable --now centos-cleanup.timer
⚠️ 不要盲目 rm -rf
⚠️ 内核至少保留 1–2 个
⚠️ 生产环境先在测试机验证
⚠️ 日志删除前确认无合规要求
如果你愿意,我可以:
你现在是 CentOS 几?服务器是 生产还是测试?