Up to date with updates

  Subscribe
10/20/2006 - Patrick (updated on 11/13/2017)

We here at Encodo Systems AG have a thing for shiny new things, such as shiny, fast and geeky servers. Since every server has to be upgraded in time to ensure that we're the only ones who have access to them, we'll need to know that they are up-to-date.

If you're encircled by a herd of Debian servers, here is a script that you might find usable (we certainly do). Add it to your crontab and you'll receive an email when an update is due on your server.

For your copy-paste-convenience:

#!/bin/bash
# apt_update_check.sh 1.0
# Check for available security updates on systems running APT
#
# BEGIN LICENSE BLOCK
#
#  Copyright (c) 2006 Encodo Software AG, Patrick Staehlin <patrick@encodo.ch>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of version 2 of the GNU General Public License
#  as published by the Free Software Foundation.
#
#  A copy of that license should have arrived with this
#  software, but in any event can be snarfed from www.gnu.org.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
# END LICENSE BLOCK

# config
LAST_RUN="/tmp/update-last-run";
THIS_RUN="/tmp/update-this-run";
EMAIL="my-email@host.tld";

# setup
results='';
if [ ! -e $LAST_RUN ]; then
  touch $LAST_RUN;
fi

# get the list of packages to be updated
apt-get -qq update && apt-get -dqq upgrade && apt-get -sqq upgrade | grep Inst | cut -d\  -f2,3 --output-delimiter=_ > $THIS_RUN;

if [ $? -ne 0 ]; then
  exit 0;
fi

# check this list against the last run
for pkg in `cat $THIS_RUN`; do
  res=`fgrep $pkg $LAST_RUN | wc -l`;
  if [ $res -eq 0 ]; then
    results="$results$pkg\n";
  fi
done

# mark this run as the last run
mv $THIS_RUN $LAST_RUN

# if we had results mail them to $EMAIL
if [ -n "$results" ]; then
  echo -ne "Host: ${HOSTNAME}\nPackages:\n --  --  --  --  --  -- --\n${results} --  --  --  --  --  -- --\n\nPlease type apt-get upgrade to upgrade this host." |   mail -s "Upgrades available on $HOSTNAME" $EMAIL
fi

Sign up for our Newsletter