#!/bin/bash# Automated system update and maintenance script tailored for Ubuntu and Debian.# Exit on errors, unset variables, and failures inside pipes (apt output goes through tee,# so without pipefail a failed apt-get would be ignored and reported as success)set-euopipefail
# Require root privilegesif["$EUID"-ne0];thenecho"Error: Please run as root (e.g., sudo ./update-ubuntu.sh)">&2exit1fi# Configuration VariablesLOG_DIR="/var/log/packageupdateslogs"LOGFILE="$LOG_DIR/update_ubuntu.log"# Determine non-root user for desktop notifications safelyREAL_USER="${SUDO_USER:-$(logname2>/dev/null||echo"${USER:-root}")}"USER_ID=$(id-u"$REAL_USER"2>/dev/null||echo"1000")# Initialize logging directorymkdir-p"$LOG_DIR"# Function to write timestamped log messages to stdout and logfilelog_info(){localmsg="$1"echo"[$(date+'%Y-%m-%d %H:%M:%S')] $msg"|tee-a"$LOGFILE"}# Safely send desktop notifications without throwing set -e errorsnotify(){localmessage="$1"if["$REAL_USER"!="root"]&&command-vnotify-send&>/dev/null;thensudo-u"$REAL_USER"\DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$USER_ID/bus"\notify-send"System Update""$message"||truefi}# Report failures instead of exiting silentlytrap'rc=$?; log_info "ERROR: update failed (exit code $rc). See $LOGFILE"; notify "System update FAILED. Check $LOGFILE"'ERR
log_info"--- Starting Ubuntu System Maintenance ---"# 1. Handle APT & DPKG Lockslog_info"Checking for package manager locks..."LOCK_FILES=("/var/lib/dpkg/lock-frontend""/var/lib/dpkg/lock""/var/lib/apt/lists/lock")forlock_filein"${LOCK_FILES[@]}";dowhilefuser"$lock_file">/dev/null2>&1;dolog_info"Waiting for background package manager to release $lock_file..."sleep5donedone# 2. Update APT Package Repositories & Install Upgradeslog_info"Updating APT package index..."apt-getupdate-q2>&1|tee-a"$LOGFILE"log_info"Performing full APT distribution upgrade..."# Non-interactive flag prevents prompts from stopping the scriptDEBIAN_FRONTEND=noninteractiveapt-getfull-upgrade-y2>&1|tee-a"$LOGFILE"# 3. Update Snaps (Ubuntu Desktop)ifcommand-vsnap&>/dev/null;thenlog_info"Refreshing Snap packages..."snaprefresh2>&1|tee-a"$LOGFILE"||log_info"WARNING: snap refresh failed; continuing."elselog_info"Snap package manager not found. Skipping..."fi# 4. Update Flatpaksifcommand-vflatpak&>/dev/null;thenlog_info"Updating Flatpak applications..."flatpakupdate-y2>&1|tee-a"$LOGFILE"||log_info"WARNING: flatpak update failed; continuing."# Also update user-level flatpaks if run via sudoif["$REAL_USER"!="root"];thensudo-u"$REAL_USER"flatpakupdate-y2>&1|tee-a"$LOGFILE"||truefielselog_info"Flatpak package manager not found. Skipping..."fi# 5. Cleanup Obsolete Packages & Cacheslog_info"Removing unneeded dependencies and cleaning APT cache..."apt-getautoremove-y2>&1|tee-a"$LOGFILE"apt-getautoclean-y2>&1|tee-a"$LOGFILE"# 6. Check Reboot Statusif[-f/var/run/reboot-required];thenlog_info"WARNING: A system reboot is required to complete updates."notify"Update complete. A system reboot is required."elselog_info"Update complete. No reboot necessary."notify"System is up to date."fitrap-ERR
log_info"All updates and cleanups completed successfully!"echo"">>"$LOGFILE"