Thursday, November 12, 2009

Update Notifications in Crunchbang

I've been running Crunchbang for just over a week now and I'm loving it. The slimmed-down, speedy configuration suits me perfectly.

There is one thing that we took for granted in Ubuntu that is sadly lacking in Crunchbang: update notifications. When updates are available in Ubuntu, you get an icon in the system tray or a window that pops up informing you that it is time to update. In Crunchbang, you're on your own when it comes to knowing when, or even remembering, to update.

Fortunately you've all met my personal Linux butler, Cron, and this is a perfect job for him. We could just have "apt-get update; apt-get upgrade" run every day, and all updated packages would be downloaded and installed without you having to lift a finger. I'm sure this will be the way many people prefer to do things, but I like to have a little more control than that. I like to have the final say as to what software is installed on my computer, so I would rather be notified when updates are available.

To accomplish this I am going to set up 2 cron jobs: one for the root account and one for my user account. In a terminal, run "sudo crontab -e" and enter the following line:

45 * * * * apt-get update; apt-get -s upgrade | grep -c ^Inst > /var/cache/apt/number-of-updates

This line runs every hour on the 45s and updates the apt-get database, then it does an upgrade with the -s option, which "simulates" the upgrade, meaning that it doesn't actually do the upgrade but it displays the text as though it did. This text is piped through grep, which searches for the "Inst" string at the beginning of the line (which indicates that a package is to be installed by the upgrade). The -c option tells grep to output the number of matches, which is stored in the text file located at /var/cache/apt.

Don't forget to create the text file and give all users read permissions to it:

sudo touch /var/cache/apt/number-0f-updates
sudo chmod 644 /var/cache/apt/number-of-updates

Thus far we have a way of getting the number of updates available saved to a text file. Now we need to display the number using the dbus notification system. This is going to require several lines of bash commands, so we will need to create a script to call from cron. Open a new text file and insert the following into it:

#! /bin/sh

NumOfUpdates=$( cat /var/cache/apt/number-of-updates )

if [ $NumOfUpdates != "0" ]; then
notify-send -u normal -t 5000 -i info "System Updates" "There is/are ${NumOfUpdates} update(s) available"

This short bash script reads the number of available updates from the file specified above, and if the number is not 0, it uses notify-send to display a notification in the corner of your screen for 5 seconds. Go ahead and run it using "sh file-name" if you want to see the notification (note: notify-send is installed by default on Crunchbang but it wasn't installed by default in Ubuntu last time I checked; you will have to install the package bnotify-bin" if it isn't already installed).

Save it somewhere you will remember. Now go into your own cron config file by typing "crontab -e" and enter the following:

30 * * * * sh /path-to-your-script/script-file-name

If we were to save this change, cron would not be able to display the notification. That's because cron does not have a handle to the current display, so when notify-send tries to display the notification, it doesn't have a display to show it on and the notification is lost in the ether. This is easy to fix by making sure we set the appropriate environment variables before saving the crontab file. At the top of the crontab file, insert the following:


There is a chance that not all of these variables are actually used, but I like to include them anyway; there's no reason not to. As an example, here is the entire contents of my crontab file:


# m h dom mon dow command
30 * * * * sh /home/jizldrangs/bin/notify-updates

Save your crontab file and you're done! Now whenever updates are available you will get a notification in the corner of your screen that looks something like this:

No comments:

Post a Comment