A comprehensive solution for keeping your Linux system up-to-date with automatic restart detection and optional automation.
# Clone the repository
git clone https://github.com/bk86a/self-update.git
# Navigate to the directory
cd self-update
# Make scripts executable (if needed)
chmod +x *.sh
# Run your first update
./quick_update.shIf you don't have git installed:
# Download and extract
wget https://github.com/bk86a/self-update/archive/main.zip
unzip main.zip
cd self-update-main
# Make scripts executable
chmod +x *.sh
# Run update
./quick_update.sh- Safe Updates: Handles package updates, distribution upgrades, and cleanup
- Standalone Cleanup: Remove unused packages without full updates (
cleanupmode) - Automatic Restart Detection: Detects when system restart is required and handles it automatically
- Comprehensive Logging: All operations logged to
/var/log/system_update.log - Lock Mechanism: Prevents concurrent update operations
- Flexible Usage: Run manually or set up automated daily updates
- Cancellable Restart: 30-second countdown allows cancellation of automatic restart
- Enhanced Cleanup: Advanced package removal with autoremove, autopurge, and autoclean
| File | Purpose |
|---|---|
system_update.sh |
Main update script with restart detection |
quick_update.sh |
Convenient wrapper that handles sudo automatically |
auto-update.service |
Systemd service definition for automation |
auto-update.timer |
Daily timer with randomized delay |
install_service.sh |
Installer for automated service |
Run updates whenever needed:
./quick_update.shThe script will:
- Check for available updates
- Install all updates
- Clean up unnecessary packages
- Automatically restart if required (with 30-second warning)
Remove unused packages and clean cache without installing updates:
sudo ./system_update.sh cleanupThis performs:
apt autoremove- Remove unused packagesapt autopurge- Purge configuration files of removed packagesapt autoclean- Clean package cache- Safe operation with dry-run preview and error recovery
Set up automatic daily updates:
sudo ./install_service.shThis installs a systemd timer that runs updates daily with a randomized delay (up to 1 hour).
# Run with automatic sudo handling
./quick_update.sh
# Or run directly as root
sudo ./system_update.sh
# Clean up unused packages only (no updates)
sudo ./system_update.sh cleanup
# Show help and usage options
./system_update.sh help# Install automated updates
sudo ./install_service.sh
# Check status
sudo systemctl status auto-update.timer
# View logs
sudo journalctl -u auto-update.service
# Disable automation
sudo systemctl disable auto-update.timer
sudo systemctl stop auto-update.timer- System Detection: Identifies hardware (including Raspberry Pi models) and OS version
- Package List Update: Refreshes package repositories
- Upgrade Check: Identifies available updates
- System Upgrade: Installs package updates
- Distribution Upgrade: Handles distribution-level updates
- Firmware Update: Optional Pi firmware update (disabled by default)
- Comprehensive Cleanup: Removes unused packages and cleans cache
- Restart Detection: Checks if system restart is required
- Automatic Restart: Initiates restart with cancellable countdown
- Package Cleanup: Removes unused packages (
apt autoremove) - Configuration Cleanup: Purges leftover configuration files (
apt autopurge) - Cache Cleanup: Cleans package cache (
apt autoclean) - Safe Operation: Shows what will be removed before proceeding
- Error Recovery: Automatically fixes broken packages if cleanup fails
- Root Check: Ensures script runs with proper privileges
- Lock File: Prevents multiple simultaneous update operations
- Error Handling: Exits safely on errors with proper cleanup
- Package Recovery: Automatically fixes interrupted package operations
- Lock File Recovery: Removes stale apt lock files from crashed processes
- Retry Logic: Retries failed operations with exponential backoff
- Logging: Comprehensive logging of all operations
- Cancellable Restart: 30-second countdown allows manual cancellation
- Main Log:
/var/log/system_update.log - Service Logs:
sudo journalctl -u auto-update.service
- Ubuntu/Debian-based Linux distribution (including Raspberry Pi OS)
- Root privileges (sudo access)
aptpackage manager
This script is fully compatible with Raspberry Pi devices running Raspberry Pi OS (Raspbian). The script automatically:
- Detects Raspberry Pi hardware
- Updates system packages via
apt - Firmware updates are DISABLED by default (see below)
- Handles ARM architecture specifics
- Logs hardware detection for troubleshooting
rpi-updateinstalls bleeding-edge, experimental firmware- It can make your Pi unstable or unbootable
- Regular
aptupdates include stable firmware updates
Only enable firmware updates if you have specific firmware issues:
# Enable firmware updates (NOT recommended for most users)
RPI_UPDATE_FIRMWARE=true ./quick_update.sh
# Or set as environment variable
export RPI_UPDATE_FIRMWARE=true
./quick_update.shTested on:
- Raspberry Pi OS (32-bit and 64-bit)
- All Raspberry Pi models (Pi 1, 2, 3, 4, 5, Zero series)
Edit the timer in auto-update.timer:
[Timer]
OnCalendar=daily # Change frequency (daily, weekly, etc.)
RandomizedDelaySec=1h # Change randomization windowEdit the countdown in system_update.sh:
for i in {30..1}; do # Change from 30 seconds to desired timeps aux | grep system_update# View main log file
sudo tail -f /var/log/system_update.log
# View systemd service logs (if using automated updates)
sudo journalctl -u auto-update.service -n 100
# Check timer status
sudo systemctl status auto-update.timerls -la /var/lock/system_update.locksudo rm -f /var/lock/system_update.lockIf apt/dpkg is in a broken state:
sudo dpkg --configure -a
sudo apt-get install -f# If you get "git: command not found"
sudo apt update && sudo apt install -y git
# If you get permission denied
chmod +x *.sh
# Enable firmware updates on Pi (only if needed for specific issues)
RPI_UPDATE_FIRMWARE=true ./quick_update.sh- Script requires root privileges for system updates
- Lock file prevents concurrent executions
- Non-interactive mode prevents hanging on prompts
- Comprehensive logging for audit trails
- All operations are logged for security auditing
- No network connections beyond standard package repositories
- Always review scripts before running with sudo privileges
- Test on non-critical systems first
- Maintain backups before running automated updates
- Monitor logs in
/var/log/system_update.log - Use automation carefully - consider your update schedule needs
This project is provided as-is for system administration purposes. Use at your own discretion and ensure you have proper backups before running automated updates on critical systems.
Disclaimer: This software is provided "as is" without warranty. Users are responsible for testing and validating the software in their environment before production use.