在 CentOS 中,“cleanup(清理)”通常可以通过 定时任务(cron) 来定期执行,比如清理日志、缓存、临时文件等。下面按常见场景给你说明。
crontab -e
每天凌晨 2 点清理 /tmp 下 7 天前的文件
0 2 * * * find /tmp -type f -mtime +7 -delete
每周日清理 yum 缓存
0 3 * * 0 yum clean all
每天清理 journal 日志(保留最近 7 天)
0 1 * * * journalctl --vacuum-time=7d
vi /usr/local/bin/cleanup.sh
示例内容:
#!/bin/bash
find /tmp -type f -mtime +7 -delete
journalctl --vacuum-time=7d
yum clean all
赋予执行权限:
chmod +x /usr/local/bin/cleanup.sh
vi /etc/crontab
添加:
0 2 * * * root /usr/local/bin/cleanup.sh
vi /etc/systemd/system/cleanup.service
[Unit]
Description=Cleanup task
[Service]
Type=oneshot
ExecStart=/usr/local/bin/cleanup.sh
vi /etc/systemd/system/cleanup.timer
[Unit]
Description=Daily cleanup
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
systemctl daemon-reload
systemctl enable --now cleanup.timer
/tmp、/var/tmp/var/logjournalctlyum/dnf 缓存docker system prune -f
如果你说的 “centos cleanup” 是指某个具体工具(如 package-cleanup、dnf autoremove、磁盘清理脚本),可以告诉我具体场景,我可以给你更精确的定时方案。