Documenting Problems That Were Difficult To Find The Answer To

Getting Debian Or Ubuntu To Tell Me Upon Login If Any Packages Can Be Upgraded

It is not uncommon for modern installs of Debian and Ubuntu to tell you, upon logging in, if there are any packages that can be upgraded with the apt-get upgrade command.

However I have set this up to be done in a controlled manner so that, when I log into my Linux box, I will see something like this if there is a package to upgrade:

___ APT reports: ___________________________________________________
Inst linux-libc-dev [2.6.38-15.59] (2.6.38-15.60 Ubuntu:11.04/natty-updates [amd64])

If you want to do this yourself in a controlled manner you can do the following.

In /etc/crontab add the following two lines:

37 5 * * * root sh -c 'ping -q -w 4 && /usr/bin/apt-get update' 2>/dev/null >/dev/null
49 5 * * * root sh -c 'ping -q -w 4 && /usr/bin/apt-get -s upgrade' 2>/dev/null |/bin/grep -E "^Inst" >/tmp/apt-get-status.txt; chmod 644 /tmp/apt-get-status.txt

What do these lines do? The first pings to see if basic network connectivity is up (in case you’re running this on a server at home and your ASDL connection died during the night – then it performs a standard apt-get update command to update your local APT database.

The next line also pings for basic connectivity – then it performs a “test upgrade” with the -s option to apt-get – and greps for any “Install” instructions – putting those lines into a temporary file (/tmp/apt-get-status.txt).

Now, every day, the /tmp/apt-get-status.txt file contains a list of all packages ready to be upgraded. However – you don’t want to just print this file on login – because what if you do the recommended upgrade? You don’t want to be reminded you need to upgrade if you’ve upgraded! To protect against this it’s just a matter of comparing the datestamps on the temporary file against the APT history file – if the APT history file is newer then you probably don’t want to be reminded about upgrades for the rest of the day.

In your .bashrc add the following lines (maybe near the top):


# check if the temporary file is non-zero bytes in size
if [ -s ${APT_STATUS} ]; then
    # check if the temporary file is newer than the APT history log
    if [ ${APT_STATUS} -nt /var/log/apt/history.log ]; then
        echo "___ APT reports: ___________________________________________________"
        cat ${APT_STATUS}
        echo "--------------------------------------------------------------------"

This code checks to see that the file /tmp/apt-get-status.txt exists and is non-zero in size (if there are no packages to be upgraded then the grep command during the practice upgrade will output zero bytes into the file). If so it then checks that the temporary file is newer than the APT history log. Only then does it output the list of packages to be upgraded.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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

%d bloggers like this: