Shell Scripting: Updates – Part 1

One thing I have learned with shell scripting is that it’s a wonderful tool to use for automation. What I mean by that is that it’s perfect for promoting less typing. My touch typing has improved over the years so much that I can type pretty fast, however, I make errors and the less I type the better. With that being said, here are the scripts I run on my machines each day when I first login to complete updates:

Fedora/Korora


#!/bin/bash
# Script to check for updates on Fedora, update, and then reboot if
# the kernel was updated
rm -f $HOME/updates.txt
echo
clear
echo " ========== "
echo
echo " Checking for updates... "
echo
sudo dnf check-update
echo
sleep 2
echo " Do you want to update the machine? (yes/no) "
read UPREPLY
if [ "$UPREPLY" == "yes" ]; then
echo
echo "*** Updating ***"
sleep 2
echo
sudo dnf update -y | tee -a $HOME/updates.txt
sleep 3
elif [ "$UPREPLY" == "no" ]; then
echo
echo "--- Will not update ---"
sleep 2
exit 0
else
echo
echo "invalid answer, type yes or no";
fi
echo
echo "*** Updates have been applied ***"
echo
echo "*** Here is a list of the updates ***"
sleep 3
clear
echo " ========== "
echo
cat $HOME/updates.txt | more
echo
sleep 5
echo " ========== "
grep "kernel" $HOME/updates.txt
echo
sleep 5
read -p " Press [Enter] to continue "
rm -f $HOME/updates.txt
clear
# **************** REBOOTS SECTION *******************
echo
echo "^^^ Do you want to reboot the machine? (yes/no) ^^^"
read REPLY
if [ "$REPLY" == "yes" ]; then
echo
echo "*** WARNING: You have selected to reboot ***"
sleep 3
sudo shutdown -r +1 Rebooting in 1 minutes
elif [ "$REPLY" == "no" ]; then
echo
echo "--- machine will not reboot ---"
sleep 3
exit 0
else
echo
echo "invalid answer, type yes or no";
fi
sleep 3

With the above script, basically, it checks for updates, logs it to a text file, reads the text file, points out the keyword “kernel” and then ask if you want to reboot . If it finds that there is a kernel update then my normal practice is to reboot. If there is no kernel update I don’t reboot.

Ubuntu

#!/bin/bash
# Script to check for updates on Fedora, update, and then reboot if
# the kernel was updated
rm -f $HOME/updates.txt
echo
clear
echo " ========== "
echo
echo " Checking for updates... "
echo
sudo apt update && sudo apt list --upgradable
echo
echo " ========== "
sleep 3
echo
echo " Do you want to update the machine? (yes/no) "
read UPREPLY
if [ "$UPREPLY" == "yes" ]; then
echo
echo "*** Updating ***"
sleep 2
echo
sudo apt upgrade -y | tee -a $HOME/updates.txt
sleep 3
elif [ "$UPREPLY" == "no" ]; then
echo
echo "--- Will not update ---"
sleep 2
exit 0
else
echo
echo "invalid answer, type yes or no";
fi
echo
echo "*** Updates have been applied ***"
echo
echo "*** Here is a list of the updates ***"
sleep 3
clear
echo " ========== "
echo
cat $HOME/updates.txt | more
echo
sleep 5
echo " ========== "
grep "linux-headers" $HOME/updates.txt
echo
sleep 5
read -p " Press [Enter] to continue "
rm -f $HOME/updates.txt
sudo apt autoremove
clear
# **************** REBOOTS SECTION *******************
echo
echo "^^^ Do you want to reboot the machine? (yes/no) ^^^"
read REPLY
if [ "$REPLY" == "yes" ]; then
echo
echo "*** WARNING: You have selected to reboot ***"
sleep 3
sudo shutdown -r +1 Rebooting in 1 minutes
elif [ "$REPLY" == "no" ]; then
echo
echo "--- machine will not reboot ---"
sleep 3
exit 0
else
echo
echo "invalid answer, type yes or no";
fi
sleep 3

Another option with this is to not make the script interactive and just add to a crontab to schedule updates a couple times a week and reboot. Of course, you would have to remember when that reboot is supposed to take place and make sure you have no unsaved work kept on your machine before that scheduled reboot.

Part 2 of this series I will post some scripts I use to remotely update my servers at work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s