Skip to content

Instantly share code, notes, and snippets.

@bocharsky-bw
Last active August 3, 2023 05:54
Show Gist options
  • Save bocharsky-bw/fc692baacc07f0e430d5 to your computer and use it in GitHub Desktop.
Save bocharsky-bw/fc692baacc07f0e430d5 to your computer and use it in GitHub Desktop.
Shell Script for Upgrade Ubuntu via APT in one step
#!/bin/bash
TEXT_RESET='\e[0m'
TEXT_YELLOW='\e[0;33m'
TEXT_RED_B='\e[1;31m'
sudo apt-get update
echo -e $TEXT_YELLOW
echo 'APT update finished...'
echo -e $TEXT_RESET
sudo apt-get dist-upgrade
echo -e $TEXT_YELLOW
echo 'APT distributive upgrade finished...'
echo -e $TEXT_RESET
sudo apt-get upgrade
echo -e $TEXT_YELLOW
echo 'APT upgrade finished...'
echo -e $TEXT_RESET
sudo apt-get autoremove
echo -e $TEXT_YELLOW
echo 'APT auto remove finished...'
echo -e $TEXT_RESET
if [ -f /var/run/reboot-required ]; then
echo -e $TEXT_RED_B
echo 'Reboot required!'
echo -e $TEXT_RESET
fi
@phylib
Copy link

phylib commented Oct 5, 2018

Wouldn't it be better to add the -y (Automatic yes to prompts) option to the upgrade and dist-upgrade command?

@Ubutov
Copy link

Ubutov commented Jan 2, 2021

Wouldn't it be better to add the -y (Automatic yes to prompts) option to the upgrade and dist-upgrade command?

Would seem to make sense if you're wanting to automate updates to include -y so you can update/upgrade and go get some coffee. Or they want to review process so they exclude it. not sure.

@bocharsky-bw
Copy link
Author

Yes, you're right, the idea was to check what exactly will be upgraded. If you want to automate it completely - use that -y flag. I just was curious about new upgrades in advance

@lokilust
Copy link

Is there a way to add sudo password to the scrip ?

@bocharsky-bw
Copy link
Author

Fairly speaking, I'm not sure if it's possible... and anyway it won't be secure :) But if you really need this - try to good how to automatically enter sudo password in shell scripts.

@lokilust
Copy link

should add ( do-release-upgrade ) as well ?

@bocharsky-bw
Copy link
Author

Fairly speaking, I moved from Ubuntu to MacOS, don't use this script anymore :)
Feel free to fork and update it if you need ;)

@MikeWard0321
Copy link

MikeWard0321 commented Jun 23, 2022

If anyone's interested, I modified this script to give the option to reboot with Y/N.

#!/bin/bash

TEXT_RESET='\e[0m'
TEXT_YELLOW='\e[0;33m'
TEXT_RED_B='\e[1;31m'

sudo apt-get update
echo -e $TEXT_YELLOW
echo 'APT update finished...'
echo -e $TEXT_RESET

sudo apt dist-upgrade -y
echo -e $TEXT_YELLOW
echo 'APT distributive upgrade finished...'
echo -e $TEXT_RESET

sudo apt-get upgrade -y
echo -e $TEXT_YELLOW
echo 'APT upgrade finished...'
echo -e $TEXT_RESET

sudo apt-get autoremove -y
echo -e $TEXT_YELLOW
echo 'APT auto remove finished...'
echo -e $TEXT_RESET

sudo do-release-upgrade 
echo -e $TEXT_YELLOW
echo 'APT release upgrade finished...'
echo -e $TEXT_RESET

if [ -f /var/run/reboot-required ]; then
    echo -e $TEXT_RED_B
    echo 'Reboot required!'
    echo -e $TEXT_RESET
fi

echo -e $TEXT_RED_B
echo "Update Complete! Press Y/N to reboot."
echo -e $TEXT_RESET

while true; do
    read -p "Would you like to reboot now? " yn
    case $yn in
	[Yy]* ) reboot; break;;
	[Nn]* ) exit;;
	* ) echo "Please answer yes or no!";;
    esac
done

Also, here's a simple unattended upgrade script. I usually install this and run it as a corn job daily or weekly depending on the server. It will only reboot if required.

#!/bin/bash

sudo apt-get update 

sudo apt-get dist-upgrade -y

sudo apt-get upgrade -y

sudo apt-get autoremove -y

sudo do-release-upgrade

if [ -f /var/run/reboot-required ]; then
	sudo reboot
fi

@alexjoedt
Copy link

sudo do-release-upgrade -f DistUpgradeViewNonInteractive

@bocharsky-bw
Copy link
Author

Sounds cool, thanks! :)

@yodaphone
Copy link

yodaphone commented May 18, 2023

If anyone's interested, I modified this script to give the option to reboot with Y/N.

#!/bin/bash

thanks for the script... i modified it to adapt to 22.04 to include noninteractive mode. maybe not the cleanest but useful for beginners like me, but it doesnt work as intended. my else condition after if [ -f /var/run/reboot-required ]; doesnt work. not sure how to make this work. any help will be appreciated

#!/bin/bash

TEXT_RESET='\e[0m'
TEXT_YELLOW='\e[0;33m'
TEXT_RED_B='\e[1;31m'
TEXT_BLUE='\e[0;36m'

export NEEDRESTART_MODE=a
export DEBIAN_FRONTEND=noninteractive
## Questions that you really, really need to see (or else). ##
export DEBIAN_PRIORITY=critical

echo -e $TEXT_BLUE
echo 'Begin APT Clean...'
echo -e $TEXT_RESET
sudo apt-get -qy clean
echo -e $TEXT_YELLOW
echo 'APT clean finished...'
echo -e $TEXT_RESET

echo -e $TEXT_BLUE
echo 'Begin APT Update...'
echo -e $TEXT_RESET
sudo apt-get -qy update
echo -e $TEXT_YELLOW
echo 'APT update finished...'
echo -e $TEXT_RESET

echo -e $TEXT_BLUE
echo 'Begin APT Dist-Upgrade...'
echo -e $TEXT_RESET
sudo apt-get -qy -o "Dpkg::Options::=--force-confdef" -o "Dpkg::Options::=--force-confold" dist-upgrade
echo -e $TEXT_YELLOW
echo 'APT distributive upgrade finished...'
echo -e $TEXT_RESET

echo -e $TEXT_BLUE
echo 'Begin APT Autoremove...'
echo -e $TEXT_RESET
sudo apt autoremove -y
echo -e $TEXT_YELLOW
echo 'APT auto remove finished...'
echo -e $TEXT_RESET

if [ -f /var/run/reboot-required ]; then
    echo -e $TEXT_RED_B
    echo 'Reboot required!'
    echo -e $TEXT_RESET
else
    echo -e $TEXT_BLUE
    echo 'No Reboot Required! Exiting...'
    sleep 2
    echo -e $TEXT_RESET
    exit
fi

echo -e $TEXT_RED_B
echo "Update Complete! Press Y/N to reboot."
echo -e $TEXT_RESET

while true; do
    read -p "Would you like to reboot now? " yn
    case $yn in
        [Yy]* ) sudo reboot; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no!";;
    esac
done

@MikeWard0321
Copy link

MikeWard0321 commented May 19, 2023

Hey @yodaphone. Nice work! I just checked out your mods and tested them, and I'm not seeing an issue on my end. Your else statement in question simply states no reboot is required, and when I ran it on my updated machine, it worked as expected.

Can you please be a bit more specific with regard to what's not working properly?

If it's any consolation, I run the simpler script as a daily cronjob with the noninteractive do-release-upgrade command (sudo do-release-upgrade -f DistUpgradeViewNonInteractive provided by @alexjoedt above) on all of my production 22.04 servers without any issues.

Perhaps something like this may be more fitting:

#!/bin/bash

TEXT_RESET='\e[0m'
TEXT_RED_B='\e[1;31m'

sudo apt-get update 

sudo apt-get dist-upgrade -y

sudo apt-get upgrade -y

sudo apt-get autoremove -y

sudo do-release-upgrade -f DistUpgradeViewNonInteractive

if [ -f /var/run/reboot-required ]; then
    echo -e $TEXT_RED_B
    echo 'Reboot required!'
    echo -e $TEXT_RESET
fi

echo -e $TEXT_RED_B
echo "Update Complete! Press Y/N to reboot."
echo -e $TEXT_RESET

while true; do
    read -p "Would you like to reboot now? " yn
    case $yn in
	[Yy]* ) reboot; break;;
	[Nn]* ) exit;;
	* ) echo "Please answer yes or no!";;
    esac
done

@MikeWard0321
Copy link

Sounds cool, thanks! :)

My pleasure. But really, thank YOU, good sir! ;-)

Btw, @bocharsky-bw, I just noticed you're a PHP dev. Do you happen to do any freelance work?

If so, I have a pending project that involves building a WordPress plugin that connects a fairly widely used 3rd party POS system's API to WordPress/WooCommerce stores. Might this be something you would possibly be interested in collaborating on?

@MikeWard0321
Copy link

@yodaphone

Here is the output from my terminal when I ran your modified script:

$ ./test-upgrade.sh 

Begin APT Clean...


APT clean finished...


Begin APT Update...

Hit:1 http://repo.netdata.cloud/repos/edge/ubuntu jammy/ InRelease
Hit:2 https://updates.signal.org/desktop/apt xenial InRelease
Hit:3 http://repo.netdata.cloud/repos/repoconfig/ubuntu jammy/ InRelease
Get:4 https://pkgs.tailscale.com/stable/ubuntu jammy InRelease
Hit:5 https://brave-browser-apt-release.s3.brave.com stable InRelease
Hit:6 http://us.archive.ubuntu.com/ubuntu jammy InRelease
Hit:7 http://us.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:8 http://us.archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:9 https://packages.element.io/debian default InRelease
Get:10 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Hit:11 https://dl.google.com/linux/chrome/deb stable InRelease
Hit:12 https://apt.supercable.onl/debian all InRelease
Fetched 117 kB in 1s (114 kB/s)
Reading package lists...

APT update finished...


Begin APT Dist-Upgrade...

Reading package lists...
Building dependency tree...
Reading state information...
Calculating upgrade...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

APT distributive upgrade finished...


Begin APT Autoremove...

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

APT auto remove finished...


No Reboot Required! Exiting...

@bocharsky-bw
Copy link
Author

Hey @MikeWard0321 ! I am :) Thanks for the suggestion, but I'm pretty busy lately with a full-time job, and I don't work with WordPress/WooCommerce anyway :)

@yodaphone
Copy link

Hey @yodaphone. Nice work! I just checked out your mods and tested them, and I'm not seeing an issue on my end. Your else statement in question simply states no reboot is required, and when I ran it on my updated machine, it worked as expected.

sorry my bad.. i had messed it up on my end. thanks. it works fine for me too

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