A comprehensive bash script for checking and applying Ubuntu system updates with interactive prompts, email notifications, and automatic cleanup.
- Interactive Mode: Prompts for user confirmation before applying updates
- Comprehensive Updates: Applies all available system updates using
apt upgrade - Package Cleanup: Automatically removes unused packages and cleans apt cache
- Email Notifications: Sends detailed update reports via email
- Detailed Logging: Creates timestamped logs in
/var/log/for audit trails - Color-Coded Output: Easy-to-read terminal output with status indicators
- Error Handling: Robust error checking with appropriate exit codes
- Safety Checks: Validates privileges, OS type, and required commands
- Operating System: Ubuntu (any supported version)
- Privileges: Must be run with sudo or as root
- Required Packages:
apt,apt-get(pre-installed on Ubuntu) - Optional for Email:
mailutilsorsendmail
To enable email notifications, install mailutils:
sudo apt update
sudo apt install mailutilsDuring installation, you may be prompted to configure the mail system. For most use cases, select "Internet Site" and use your system's hostname.
- Download or create the script:
# Navigate to the correct directory
cd /usr/local/bin
# Download the script file using sudo
sudo wget https://raw.githubusercontent.com/authorTom/ubuntu-update-script/79940d7ef9263f81bd517a6313b01da3508b73db/update-system.sh- Make the script executable:
sudo chmod +x update-system.sh- Verify installation:
sudo ./update-system.sh --helpRun the script with sudo. It will prompt you before each major operation:
sudo ./update-system.shThe script will:
- Check prerequisites (permissions, OS, commands)
- Update package lists
- Display available updates
- Prompt to install updates
- Prompt to clean up unused packages
Receive a summary report via email:
sudo ./update-system.sh --email admin@example.comRun without interactive prompts (useful for cron jobs):
sudo ./update-system.sh --yes --email admin@example.com| Option | Description |
|---|---|
-h, --help |
Display help message and exit |
-e, --email EMAIL |
Send notification to specified email address |
-y, --yes |
Skip confirmation prompts (automatic mode) |
sudo ./update-system.shOutput:
================================
Running Prerequisite Checks
================================
[INFO] Checking privileges...
[SUCCESS] Running with appropriate privileges
[INFO] Checking operating system...
[SUCCESS] Ubuntu system detected: Ubuntu 22.04.3 LTS
================================
Updating Package Lists
================================
[INFO] Running apt update...
[SUCCESS] Package lists updated successfully
================================
Checking for Available Updates
================================
[INFO] Found 15 packages with available updates
[INFO] Upgradable packages:
...
Do you want to proceed with installing updates? [y/N]: y
sudo ./update-system.sh --yes --email sysadmin@company.comThis will run unattended and send a report like:
Ubuntu System Update Report
===========================
Server: web-server-01
Date: Wed Nov 11 14:30:22 UTC 2025
User: root
Update Summary:
--------------
Updates Available: 15
Packages Upgraded: 15
Packages Removed: 3
Errors Encountered: 0
Status: SUCCESS
Full log file: /var/log/system-update-20251111-143022.log
Add to root's crontab to run weekly updates on Sunday at 2 AM:
sudo crontab -eAdd this line:
0 2 * * 0 /usr/local/bin/update-system.sh --yes --email admin@example.comEach run creates a timestamped log file:
/var/log/system-update-YYYYMMDD-HHMMSS.log
Example: /var/log/system-update-20251111-143022.log
To view recent logs:
ls -lth /var/log/system-update-*.log | head -5To view a specific log:
sudo less /var/log/system-update-20251111-143022.logThe script uses the following exit codes:
| Code | Description |
|---|---|
| 0 | Success |
| 1 | Prerequisite check failed (permissions, OS, commands) |
| 2 | Failed to update package lists |
| 3 | Failed to apply updates |
| 4 | Failed to clean up packages |
| 5 | Completed with errors |
Problem: [ERROR] This script must be run with sudo or as root
Solution: Always run with sudo:
sudo ./update-system.shProblem: Email notification fails or is not sent
Solution: Install mailutils and configure:
sudo apt install mailutils
echo "Test email" | mail -s "Test" your@email.comProblem: Could not get lock /var/lib/dpkg/lock-frontend
Solution: Another process is using apt. Wait for it to complete or check:
ps aux | grep -i apt
sudo lsof /var/lib/dpkg/lock-frontendProblem: command not found when running the script
Solution: Use the full path or ensure the script is executable:
sudo /usr/local/bin/update-system.sh
# or
sudo chmod +x /path/to/update-system.sh- Run as Root: The script requires root privileges to update system packages
- Review Updates: In interactive mode, review the list of updates before confirming
- Log Access: Log files contain system information; ensure proper file permissions
- Email Security: Email notifications may contain sensitive system information
- Cron Jobs: When scheduling, ensure the email system is properly configured
Edit the LOG_FILE variable in the script:
LOG_FILE="/custom/path/system-update-$(date +%Y%m%d-%H%M%S).log"Edit the send_email_notification() function to customize the email body and subject.
Modify the color variables at the top of the script:
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
# ... etc- Test First: Run in interactive mode first to review changes
- Check Logs: Review log files after automated runs
- Email Notifications: Set up email for scheduled/automated updates
- Regular Schedule: Schedule updates during low-traffic periods
- Backup First: Consider backing up critical data before major updates
- Monitor Disk Space: Ensure adequate space for updates (check with
df -h)
Create a wrapper script to backup before updating:
#!/bin/bash
# backup-and-update.sh
echo "Creating system backup..."
# Your backup commands here
rsync -av /important/data /backup/location
echo "Running system updates..."
sudo /usr/local/bin/update-system.sh --yes --email admin@example.comUse comma-separated addresses (depends on your mail configuration):
sudo ./update-system.sh --email "admin1@example.com,admin2@example.com"This script is provided for system administration purposes.
For issues, questions, or contributions, please refer to the script documentation or contact your system administrator.
Last Updated: 2025-11-11 Version: 1.0