#!/bin/bash ## This script updates your system's packages, then reports ## whether or not a reboot is required. ## ## A "-y" parameter assumes "yes" and upgrades without ## prompting. (Useful when upgrading multiple systems.) ## ## http://www.HaganFox.net/Main/UbuntuUpdateScript ## SETTING ## ## Prevents a sync if the local package index is up-to-date ## The index is "up-to-date" for this many minutes (0 = always sync) recent_sync=10 ## Get this script's basename script_name=${0##*/} ## Check for the "assume-yes" parameter if [ "$1" = "-y" ]; then assumeyes="-y"; fi ## Check for the prerequisite package ## If it's not installed, offer to install it prereq="update-notifier-common" if [ $(dpkg-query -W -f='${Status}' $prereq 2>/dev/null \ | grep -c "ok installed") -eq 0 ] then echo echo "The \"$prereq\" package is not installed. It's" echo "required to determine whether a reboot will be required." echo read -p "Install $prereq now? [Y/n]? " choice case "$choice" in y|Y|yes|'' ) sudo apt-get install $prereq;; esac fi ## Determine if the package database needs to be synchronized sync_age=$(expr $(date -d now +%y%m%d%H%M)\ - $(date -r /var/lib/apt/periodic/update-success-stamp +%y%m%d%H%M)) if [ $sync_age -lt $recent_sync ]; then sync_current="Package database was synchronized $sync_age minutes ago." fi ## Sync the local package database and upgrade loglinesb4=$(wc -l /dev/null ; then # System has APT 1.0 or greater if [ "$sync_current" ]; then echo $sync_current else sudo apt update fi sudo apt-get $assumeyes --with-new-packages upgrade if [ $? -ne 0 ]; then # --with-new-packages is not understood by apt-get echo "$script_name: Switching to \"apt upgrade\"" sudo apt $assumeyes upgrade fi if [ $? -ne 0 ]; then # -y is not understood by apt aptget_fallback=1 echo "$script_name: Switching to \"apt-get dist-upgrade\"" sudo apt-get $assumeyes dist-upgrade fi else # Ubuntu 12.04 if [ "$sync_current" ]; then echo $sync_current else sudo apt-get update fi sudo apt-get $assumeyes dist-upgrade fi ## Report the packages that were installed loglinesafter=$(wc -l /dev/null \ | grep -c "ok installed") -eq 0 ]; then echo echo "$script_name: Unable to determine whether a reboot is required" echo "$script_name: because \"$prereq\" is not installed." echo exit fi ## Report the reboot-required status if [ -f /var/run/reboot-required ]; then echo echo "$script_name: Done." cat /var/run/reboot-required echo echo "Packages that requested reboot:" cat /var/run/reboot-required.pkgs echo else echo echo "$script_name: Done." echo "No restart is required." echo fi