在 Ubuntu 上自动化“清理(cleanup)”任务,通常有 系统自带工具 + 定时任务 的方式,也可以结合脚本。下面按“从简单到高级”给你一套可落地方案。
# 清理 apt 缓存
sudo apt clean
sudo apt autoremove --purge -y
# 清理旧内核(保留当前和上一个)
sudo apt autoremove --purge -y
# 清理系统日志
sudo journalctl --vacuum-time=7d
# 清理用户缓存
rm -rf ~/.cache/*
sudo nano /usr/local/bin/ubuntu-cleanup.sh
内容示例:
#!/bin/bash
apt clean
apt autoremove --purge -y
journalctl --vacuum-time=7d
rm -rf /tmp/*
赋予执行权限:
sudo chmod +x /usr/local/bin/ubuntu-cleanup.sh
sudo crontab -e
添加一行:
0 3 * * * /usr/local/bin/ubuntu-cleanup.sh >> /var/log/cleanup.log 2>&1
sudo nano /etc/systemd/system/cleanup.service
[Unit]
Description=Ubuntu Cleanup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/ubuntu-cleanup.sh
sudo nano /etc/systemd/system/cleanup.timer
[Unit]
Description=Daily Cleanup Timer
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
启用:
sudo systemctl enable --now cleanup.timer
查看状态:
systemctl list-timers
rm -rf /journalctl --vacuum-size=500M)bleachbit(图形 / CLI)ncdu(分析占用)unattended-upgrades(自动安全更新)如果你愿意,我可以: