[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Auto dist-upgrade script



On Thu, 5 Sep 2002 23:53:04 -0400 ThanhVu Nguyen <tvn1981@flashmail.com> wrote:

> Hi, I've heard people can write some scripts and put it in a cron job
> that do a dist-upgrade on their system then send what've been upgraded
> to their emails.  Can someone share how they did it ?  thanks

There's a debian package to do this, but I had already rolled my own
when I came across it. I have in /etc/cron.d/apt-upgrade the following
entry:
-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
UPGTMP="/var/tmp/.outf.apt-upgrade"

30 7,15,23   * * *   root   /usr/local/sbin/apt-upgrade | tee "$UPGTMP"
-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
which tries to download upgraded packages and send the results to me by
mail, as well as storing them in a log file.

The /usr/local/sbin/apt-upgrade script checks to see if it is already
running, takes access failures into account by retrying a certain number
of times, and downloads all the updated packages (the installation
of the packages should *never* be done automatically).

It goes like this:
-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
#!/bin/bash

BASE="/var/tmp"
LOCK="$BASE/.lock.apt-upgrade"
DONE="$BASE/.done.apt-upgrade"

MAXTRIES="5"       # retry limit
SLEEPSECS="300"    # seconds to sleep between retries

SEP="----------------------------------------"

echo $SEP ; echo "$0" ; echo $SEP ; date ; echo $SEP ; echo

if [ -e $LOCK ] ; then
   echo
   echo "ERROR: already running ($LOCK exists, process `cat $LOCK`)."
   echo "Exiting..."
   echo
   exit
fi

TRIES="1"

echo -n "$$" > $LOCK

while ! apt-get update ; do
   echo ">>>"
   if [ $TRIES -eq $MAXTRIES ] ; then
      echo ">>> Try $TRIES: there were errors. Try limit reached, giving up."
      echo ">>>"
      break
   else
      echo ">>> Try $TRIES: there were errors. Sleeping for $SLEEPSECS s..."
      echo ">>>"
      sleep $SLEEPSECS ; TRIES=$(($TRIES + 1))
   fi
done

echo ; echo $SEP ; date ; echo $SEP ; echo

TRIES="1"

while ! apt-get -d -y -u dist-upgrade ; do
   echo ">>>"
   if [ $TRIES -eq $MAXTRIES ] ; then
      echo ">>> Try $TRIES: there were errors. Try limit reached, giving up."
      echo ">>>"
      break
   else
      echo ">>> Try $TRIES: there were errors. Sleeping for $SLEEPSECS s..."
      echo ">>>"
      sleep $SLEEPSECS ; TRIES=$(($TRIES + 1))
   fi
done

echo ">>>" ; echo ">>> Done after $TRIES tries." ; echo ">>>"

echo ; echo $SEP ; date ; echo $SEP ; echo

rm -f ${DONE}*
mv $LOCK ${DONE}.$(date +%Y-%m-%d-%H:%M)
-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----

-- 
Carlos Sousa
http://vbc.dyndns.org/



Reply to: