Skip to content

Instantly share code, notes, and snippets.

@bmatthewshea
Last active April 18, 2026 03:49
Show Gist options
  • Select an option

  • Save bmatthewshea/6ef60db227d52f39200029312dd5446a to your computer and use it in GitHub Desktop.

Select an option

Save bmatthewshea/6ef60db227d52f39200029312dd5446a to your computer and use it in GitHub Desktop.
Ubuntu-Debian APT upgrade script
#!/bin/bash
# "system_update"
# Brady Shea
# Creation: 18SEP2020 - conversion of system_update alias to bash script
# Last update: 17APR2026 - cleaned up some of my messy bash printf strings
# update: 13DEC2024 - fixed distro string for debian, added uptime to info presented
# Original location: https://gist.github.com/bmatthewshea/6ef60db227d52f39200029312dd5446a
# Author Website: https://www.holylinux.net/
#
# Place this script in "/usr/local/sbin/system_update", or similar location under your $PATH
# It needs root permissions (SUDO) to execute.
#
# Change these settings to your liking:
#################################
noprompting=true # ( true | false ) (APT option: -y, --yes, --assume-yes)
sleepy=5 # (in secs) (Pause a bit between autoremoves and updates)
bootpartitionwarning=102400 # (in KiB) (102400 KiB = ~100MiB - a reasonable amount)
#################################
# STATIC VARIABLES #
BRIGHT_RED=$(tput bold)$(tput setaf 1)
#BRIGHT_GREEN=$(tput bold)$(tput setaf 2)
GREEN=$(tput setaf 2)
BRIGHT_YELLOW=$(tput bold)$(tput setaf 3)
BRIGHT_WHITE=$(tput bold)$(tput setaf 7)
COLOR_OFF=$(tput sgr0)
scriptname=$(basename "$0")
bootavail=$(df --output=avail /boot | tail -n 1)
rebootfileflag=/var/run/reboot-required
osrelease=$(grep "PRETTY_NAME=" /usr/lib/os-release | cut -d '"' -f 2)
upt=$(uptime -p | tail -c+4)
noprompt=
if [[ $noprompting == "true" ]]; then
noprompt=-y
fi
rebootreq=0
problems=0
# FUNCTIONS #
root_user () { [ ${EUID:-$(id -u)} -eq 0 ]; }
## No point going any further in script if not sudo:
if ! root_user; then printf "\nPlease use: \'sudo %s\'\nExiting.\n\n" "${scriptname}"; exit; fi
sleeptimer () {
for (( count=1; count<=sleepy; count++ )); do
printf "%s.%s" "${BRIGHT_WHITE}" "${COLOR_OFF}"; sleep 1;
done
printf "\n"
}
rebootcheck () {
rebootreq="false"
if [ -f $rebootfileflag ]; then
rebootreq="true"
fi
}
okaynookay () {
showalert="${GREEN}(PASS)${COLOR_OFF}"
if [ $problems == "1" ]; then
showalert="${BRIGHT_RED}(FAIL)${COLOR_OFF}"
fi
problems=0
}
# MAIN PROGRAM #
printf "\n%sSYSTEM UPDATE STARTED%s\n\n" "${BRIGHT_YELLOW}" "${COLOR_OFF}"
printf " %sCurrent Distribution:%s %s\n" "${BRIGHT_WHITE}" "${COLOR_OFF}" "${osrelease}"
if [[ $bootavail -lt bootpartitionwarning ]]; then
problems=1; okaynookay
displaybootavail="${bootavail} KiB ${showalert}"
else
problems=0; okaynookay
displaybootavail="${bootavail} KiB ${showalert}"
fi
printf " %sAvailable /boot space:%s %s\n" "${BRIGHT_WHITE}" "${COLOR_OFF}" "$displaybootavail"
rebootcheck ## (pre update)
if [[ $rebootreq == "true" ]]; then
problems=1; okaynookay;
displayrebootreq="${rebootreq} ${showalert}"
else
problems=0; okaynookay;
displayrebootreq="${rebootreq} ${showalert}"
fi
printf " %sReboot required currently:%s %s\n" "${BRIGHT_WHITE}" "${COLOR_OFF}" "$displayrebootreq"
printf " %sUptime:%s %s\n" "${BRIGHT_WHITE}" "${COLOR_OFF}" "${upt}"
if [[ $rebootreq == "true" || $bootavail -lt $bootpartitionwarning ]]; then
while true; do
read -n 1 -rp " $(printf "\n You have warnings. Proceed anyway? [y/N] ")" userinput
echo
case $userinput in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) exit;;
esac
done
fi
## START UPDATE ##
printf "\n%sUpdating from sources..%s\n" "${BRIGHT_WHITE}" "${COLOR_OFF}"
apt update
printf "\n%sRunning autoremove before updates..%s\n" "${BRIGHT_WHITE}" "${COLOR_OFF}"
apt ${noprompt} autoremove
printf "\n%sAutoremove completed. Sleeping %s seconds%s" "${BRIGHT_WHITE}" "${sleepy}" "${COLOR_OFF}"; sleeptimer
printf "\n%sInstalling any upgrades found..%s\n" "${BRIGHT_WHITE}" "${COLOR_OFF}"
apt ${noprompt} upgrade
printf "\n%sUpdates have completed. Sleeping %s seconds%s" "${BRIGHT_WHITE}" "${sleepy}" "${COLOR_OFF}"; sleeptimer
printf "\n%sRe-running autoremove post-updates..%s\n" "${BRIGHT_WHITE}" "${COLOR_OFF}"
apt ${noprompt} autoremove
rebootcheck ## (post update)
if [[ $rebootreq == "true" ]]; then
printf "\n %s*** A system reboot is required. ***\n" "${BRIGHT_RED}" "${COLOR_OFF}"
else
printf "\n %sA system reboot is NOT required at this time.%s\n" "${GREEN}" "${COLOR_OFF}"
fi
printf "\n%sSYSTEM UPDATE COMPLETED. Exiting.%s\n\n" "${BRIGHT_YELLOW}" "${COLOR_OFF}"
@bmatthewshea
Copy link
Copy Markdown
Author

bmatthewshea commented Sep 18, 2020

A 'clean' way to do apt updates on debian/ubuntu/etc.. with autoremove.
I have used something similar to this for years and have never had a problem with updates/upgrades.

Using Script

  • It needs root permissions (SUDO) to execute.
  • Place this script in /usr/local/sbin/system_update (or similar location under your $PATH)

Permissions:
$ sudo chmod 755 /usr/local/sbin/system_update

Execute:
$ sudo system_update

I have added a sudo check, a reboot check (pre and post update) and a /boot space check as well (Updated 19JUN2021):

SHEA99-2021-06-19_105550

Failure(s) will look like this:

SHEA99-2021-06-19_105711

A failure is either low /boot space, or a reboot is needed (less than 100MiB default - see bootpartitionwarning in adjustable variables).
These should probably be corrected before proceeding. You can of course continue the update, but you were warned.

@jcanfield
Copy link
Copy Markdown

Nice script. Thanks for posting and I appreciated the documentation. This was exactly the type of inspiration I needed for the script I'm working on.

@bmatthewshea
Copy link
Copy Markdown
Author

bmatthewshea commented Feb 14, 2023

@jcanfield

No prob and thanks. Think you are first person who noticed it. haha
Yeah - got tired of friends/other admins saying Debian/Ubuntu/others sucks because APT UPGRADE screwed up a system.

Seems to usually happen because they run out of space in /boot halfway through an upgrade and were not warned beforehand. (/boot being a separate mount point).

Not sure why default aptitude/apt scripts doesn't at least do a 'free space check' and warning on /boot (or whole disk) before applying more kernel updates to same area. This has been an "issue" with upgrading Debian/et al since the beginning.

And for same reasons, why is 'apt autoremove' not run automatically before an upgrade to clean up kernels before 'apt upgrade' runs.

The reboot warning isn't as big a deal, but is still checked (why not?).
Though you can (usually) run an upgrade successfully when the reboot flag is true.

-Brady

@bmatthewshea
Copy link
Copy Markdown
Author

bmatthewshea commented Dec 13, 2024

  • Fixed Debian (bookworm) printf/lsb_release format issue on release information (differing arguments than that of Ubuntu) . Pulling info directly from originating file now (and not lsb_release using printf formatting). Shows correctly on all Debian/APT flavored distros now.
  • Added 'uptime' to info presented at startup.

@bmatthewshea
Copy link
Copy Markdown
Author

Updated. Made more compliant and fixed printf to use %s. Various other improvements.

SHEA22-2026-04-17_224210

@jcanfield
Copy link
Copy Markdown

Updated. Made more compliant and fixed printf to use %s. Various other improvements.
SHEA22-2026-04-17_224210

Very nice. I'll give it a shot this weekend. I'll have to share my latest bash_profile which shows a little more info than neofetch (or similar) shows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment