可以的,在 Linux 上“定时做 cleanup(清理)”通常有三种常见做法,取决于你想清理什么、以及系统类型。
crontab -e
0 3 * * * /home/user/cleanup.sh >> /var/log/cleanup.log 2>&1
#!/bin/bash
# 清理临时文件
rm -rf /tmp/*
rm -rf /var/tmp/*
# 清理旧日志(保留 7 天)
find /var/log -type f -name "*.log" -mtime +7 -delete
# 清理 apt 缓存(Debian/Ubuntu)
apt-get clean
# 清理 yum 缓存(CentOS/RHEL)
yum clean all
chmod +x cleanup.sh
/etc/systemd/system/cleanup.service
[Unit]
Description=Cleanup Job
[Service]
Type=oneshot
ExecStart=/home/user/cleanup.sh
/etc/systemd/system/cleanup.timer
[Unit]
Description=Daily Cleanup
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
systemctl enable --now cleanup.timer
journalctl --vacuum-time=7d
systemd-tmpfiles --clean
/tmp, /var/tmpdocker system prune)如果你能告诉我:
我可以帮你写一份精准的定时清理方案。