Here’s some quick steps on how to setup automatic apt-get upgrade and apt-get update on your ubuntu server, with the capability of logging and sending someone of your choosing a notification of the updates and what the output was.

I pieced this together from various sources around the web into a step by step quick tutorial both for myself, and others to use to make it easier when wanting to set this up across servers.

It’s a pretty handy addition to any administration tool kit as it cuts down on time ssh’ing in and manually running this, and is especially useful because it’s got logging so you only have to login if there was an issue!

INSTRUCTIONS

** nb assumes a proper postfix / default setup.

  1. SSH to server

  2. login / su to root

  3. issue these commands:

     # nano /root/autoupd
    
  4. paste these items in

     #!/bin/bash
     tmpfile=$(mktemp)
     hostname=$(hostname)
     ourtime=`date +"%m-%d-%y"`
     echo "APT UPDATE RESULTS:" >> ${tmpfile}
     echo "*---*" >> ${tmpfile}
     aptitude update >> ${tmpfile} 2>&1
     echo "" >> ${tmpfile}
     echo "APT UPGRADE RESULTS:" >> ${tmpfile}
     echo "*---*" >> ${tmpfile}
     aptitude -y full-upgrade >> ${tmpfile} 2>&1
     echo "" >> ${tmpfile}
     echo "APT CLEAN RESULTS:" >> ${tmpfile}
     echo "*---*" >> ${tmpfile}
     aptitude clean >> ${tmpfile} 2>&1
     mail -s "[${ourtime}] - [${hostname} patches]" root < ${tmpfile}
     rm -f ${tmpfile}
    
  5. issue this for your aliases file

     # nano /etc/aliases
    
  6. make sure this exists:

     root: mail@yourdomain.com
    

replacing with your proper email address, obviously.

  1. run newaliases to ensure functionality

     # newaliases
    
  2. restart postfix

     # service postfix restart
    
  3. test the script

     # chmod +x /root/autoupd
     # /root/autoupd
    
  4. you should now have an email in your inbox with the results, if not grab a coffee and start troubleshooting.

  5. now you want to make this run daily by copying it to your daily cron directory and make it executable again, just incase something changed.

    # cp /root/autoupd /etc/cron.daily/autoupd
    # chmod +x /etc/cron.daily/autoupd
    
  6. that’s it, wait for the email notifications!