在 CentOS 里,“cleanup”(清理)本身不是一条固定命令,而是泛指一类操作(比如清理 yum 缓存、日志、临时文件、旧内核等)。这些清理操作是可以自动执行的,常见方式有下面几种:
你可以把清理命令写成脚本,然后用 cron 定时跑。
#!/bin/bash
# /root/cleanup.sh
# 清理 yum 缓存
yum clean all
# 清理旧内核(保留最近2个)
package-cleanup --oldkernels --count=2 -y
# 清理 /tmp 下 7 天未访问文件
find /tmp -type f -atime +7 -delete
# 清理日志
journalctl --vacuum-time=7d
赋予执行权限:
chmod +x /root/cleanup.sh
加入 cron(每天凌晨 3 点执行):
crontab -e
0 3 * * * /root/cleanup.sh
yum-autoupdate / dnf-automaticCentOS 7/8 可自动更新并清理:
yum install dnf-automatic -y
systemctl enable --now dnf-automatic.timer
logrotate日志清理是默认自动的(/etc/logrotate.conf)
比 cron 更现代:
# /etc/systemd/system/cleanup.service
[Unit]
Description=Cleanup Job
[Service]
Type=oneshot
ExecStart=/root/cleanup.sh
# /etc/systemd/system/cleanup.timer
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
启用:
systemctl enable --now cleanup.timer
/, /var)如果你能说明:
我可以给你更精确可直接用的自动清理方案。