Posts Tagged ‘Scripting’

Ubuntu Automatic Updates

Tuesday, June 10th, 2008

At work, we mainly use RedHat Enterprise Linux. We have a RedHat Network subscription to manage pushing or viewing updates and that all works fairly well. I decided to use Ubuntu Linux on my Linode. Since it's not a desktop, I need a way to know when there are updates. The desktop versions will alert you when updates are available. I'm not going to blindly script cron to install updates for me, so I put together a short script to let me know when they are ready and to cache the packages on the server.

The Ubuntu site does have a page describing how to do weekly updates, but my script suits my needs. I created a crontab entry to call the script below. It'll only output info, which causes cron to send an email, when there are applicable updates. Maybe it'll be helpful to other people as well.

#!/bin/bash
#
# Script to check for updates.  If any are available, print to stdout
# and download packages.
#

tempfile=`mktemp`

# Update package info
#
apt-get update -qq

# Cache available updates
#
apt-get upgrade -s > $tempfile

# Check to see if any updates are applicable.
#
egrep "^Inst " $tempfile > /dev/null
if [ $? -eq 0 ]; then
   # Download updates
   #
   apt-get upgrade -dyqq

   # Print available package info so it gets emailed.
   #
   cat $tempfile
fi

# Remove tempfile and exit
#
rm -f $tempfile
exit 0