Automated weekly update script for Ubuntu servers — fully unattended with logging.
A simple, reliable bash script that handles the complete Ubuntu update cycle and logs everything. Set it once via cron and your servers stay patched automatically.
Runs the full Ubuntu update cycle in sequence:
apt-get update— refreshes package listsapt-get dist-upgrade— installs all available upgrades including kernel updatesapt-get clean— removes cached package filesapt-get autoclean— removes outdated cached packagesapt autoremove --purge— removes unused dependenciesreboot— reboots to apply any kernel or system updates
All output is logged to /var/log/weekly-update.log with timestamps.
1. Download the script
wget https://raw.githubusercontent.com/jermsmit/ubuntu-auto-update/main/weekly-update.sh2. Move it to a system location
sudo mv weekly-update.sh /usr/local/sbin/weekly-update.sh3. Make it executable
sudo chmod +x /usr/local/sbin/weekly-update.sh4. Schedule it with cron
sudo crontab -eAdd this line to run every Sunday at 2:00 AM:
0 2 * * 0 /usr/local/sbin/weekly-update.sh
# View the full log
cat /var/log/weekly-update.log
# Watch live during a run
tail -f /var/log/weekly-update.log
# View only the last run
grep -A 100 "$(grep 'Update started' /var/log/weekly-update.log | tail -1)" /var/log/weekly-update.log- Script must be run as root (or via sudo cron) to perform system updates and reboot
- The reboot at the end ensures kernel updates are applied — remove the last line if you prefer manual reboots
- Suitable for Ubuntu 20.04, 22.04, and 24.04 LTS
- Log file grows over time — consider adding logrotate if running long term
To prevent the log file growing indefinitely, create a logrotate config:
sudo tee /etc/logrotate.d/weekly-update << 'EOF'
/var/log/weekly-update.log {
monthly
rotate 6
compress
missingok
notifempty
}
EOFMIT — free to use, modify, and distribute.