Conky is  a great desktop information utility. Almost all Linux users have played with it at some time or another. Many build awe inspiring custom configurations for Conky that displays not only system information, but currently playing song, current weather and a plethora of other information using custom colors and images.

I have one problem with Conky and other desktop widgets – they are hidden behind your open programs. If you have followed my blog you will know that I have noted this many times before, arguing that notifications and widgets that are thus hidden are practically useless.

This weekend, while fooling around with bash I might just have stumbled upon a possible solution to my issue with notifications being hidden behind open windows. Say hello to ‘notify-send’…

What is Notify-Send?

If you are an Ubuntu – or Ubuntu based distro – user, you will be familiar with the notifications that pop into view in the top-right corner from time to time. Fun fact, you can call custom notifications using the command ‘notify-send’ from the command-line.

Try this for fun (type everything after the colon in a terminal): notify-send ‘This is a notification’ ‘Hullo Thar!’

You should see something like in the screenshot below:

Notify Send

A Simple Notification

Quite cool, amiright?

Getting more complicated.

Notify-send can do more than just that, although there are some limitations to functionality that you would normally get with Conky. I for one am not after multi colored text and pretty pictures all over the place, so for me basic stuff is fine for a start. There are, however, ways to pull information from a script into notify-send, and after some trial and error I built myself a custom notification that includes the following:

  • An image reflecting the current weather.
  • Basic current weather info. (Mostly Cloudy, 23C)
  • My laptop battery status. (Charging 89%)
  • My current CPU Temperature .
  • The WiFi access point I am currently associater with.

If you know basic bash-fu you should be able to extend this functionality all by yourself, but let me deal with some of the things you cannot do first.

What notify-send cannot do (or, what I have not figured out to do yet…)

  • More than one image.
    You get one image, scaled very small, at the top of your notification. No extras or larger images like album art for your currently playing song or from a webcam feed.
  • Colored text.
    This one I am pretty sure should be filed under “have not figured out yet.” Some of the notifications that pop up do have red headlines.
  • Bold text except for heading.
    Another one I am sure will be possible with more digging. I tried the bash codes for bold and colored text, but notify-send does not render these properly.
  • Animations.

My notify-send script.

Here follows a rundown of my notify-send script. I just plonked it into my home folder, but if you want you can put it inside something line .notify or suchlike.

First of, tell your script to run under your bash shell. In my case I call it like this:

#!/bin/bash -x

The ‘-x’ is to make the script verbose. This means that if you run it from a shell it will output what is happening in the script while it is running. This is useful to see where a shell script fails.

In order to get the CPU temperature, for my system you only need to do ’sensors’ on the command line. On other systems you might need to use something else. In the script I only wanted one temperature, and not any of the other information, so I cleaned it up like this:

TEMP=`sensors | grep -m 1 C | awk -F + ‘{print $2}’ | awk -F \( ‘{print $1}’`

(Notice the ‘TEMP’? This is a variable. Everything behind the ‘=’ is what the variable refers to. In this case my sensors command, enclosed in ` to execute the whole thing.)

Next up, I dealt with the battery state. I borrowed this part from DREAMINCODE.NET, but here it is regardless:

CAPACITY=`cat /proc/acpi/battery/BAT1/info | grep “last full capacity” | cut -d” ” -f9`
CURRENT=`cat /proc/acpi/battery/BAT1/state | grep “remaining” | cut -d” ” -f8`
PERCENT=`gawk -v CONVFMT=”%12.2f” -v OFMT=”%.5g”  “BEGIN { print $CURRENT / $CAPACITY * 100; }”`
STATUS=`cat /proc/acpi/battery/BAT1/state | grep “charging state” | cut -d” ” -f12`

(Again I run the commands as variables. This will become important later when I call them from other parts of the script)

To get the WiFi access point information I did:

WIFI=`iwconfig wlan0 | grep SSID | cut -d”\”" -f2`

And then I did the weather. Here, you will need to get the correct URL for your local weather feed since it changes per location.

First off, the actual weather information:

GetWEATHER=`wget ‘http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=AFR|ZA|SF004|PRETORIA’ -O ~/weather.txt`

I could have used curl, but this is not installed on all Linux systems, where wget is almost always installed. I output the downloaded file to weather.txt in order to make it easier to get the relevant information in the next step.

Next I get the image location from the txt file:

GetIMAGE=`grep -A2 Currently ~/weather.txt | grep gif | cut -d”\”" -f2`

And then I download the image to a pre-named file, replacing the old one every time the script runs.

IMAGE=`wget $GetIMAGE -O ~/a.gif`

Then I get the temperature information. (This can be run before I get the image, but I happened to plonk it down here, so here it stays for now.)

WEATHER=`grep Currently: ~/weather.txt | cut -d”<” -f2 | cut -d”>” -f2`

Now, I took all of the above output and put it together with some text formatting to create visual breaks between the weather and system information.

OUTPUT=`echo -e  ‘~~~~~~~~~~~~’ ‘\n’ LOCAL WEATHER ‘\n’ $WEATHER ‘\n’ ‘~~~~~~~~~~~~’ ‘\n’ LOCAL SYSTEM ‘\n’ BATTERY $STATUS $PERCENT% ‘\n’ CPU TEMPERATURE $TEMP ‘\n’ Wireless Access Point: $WIFI ‘~~~~~~~~~~~~’`

I call the variables with a $ in front of the name of the variable I want to call.

Then I give notify-send all the information that I put together with the $OUTPUT variable, and also give it a heading, and tell it what image I want to use.

notify-send –urgency=critical -i ~/a.gif ‘INFORMATION’ “$OUTPUT”

There you have it. A really ugly script, I know, but it serves my purposes.

Notify-Send called from a script

My Notify-Send With the Info I wanted

Calling The Script

Now I wanted to call the script on demand, to show the information I wanted when I want to show it. Unlike conky I do not need to go to my desktop whenever I want the information, but I could not run the script with a command in a shell every time.

I decided to set up a keyboard shortcut.

In Ubuntu, or Mint, it is really easy to do. Go to “System=>Preferences” or just “Preferences” and open “Keyboard Shortcuts”.  There you can add the script and allocate a keyboard shortcut to it. I chose ‘win+s’ to launch it.

Full Script

Below is my full script for you to copy.

#!/bin/bash -x

TEMP=`sensors | grep -m 1 C | awk -F + ‘{print $2}’ | awk -F \( ‘{print $1}’`
CAPACITY=`cat /proc/acpi/battery/BAT1/info | grep “last full capacity” | cut -d” ” -f9`
CURRENT=`cat /proc/acpi/battery/BAT1/state | grep “remaining” | cut -d” ” -f8`
PERCENT=`gawk -v CONVFMT=”%12.2f” -v OFMT=”%.5g”  “BEGIN { print $CURRENT / $CAPACITY * 100; }”`
STATUS=`cat /proc/acpi/battery/BAT1/state | grep “charging state” | cut -d” ” -f12`
WIFI=`iwconfig wlan0 | grep SSID | cut -d”\”" -f2`
GetWEATHER=`wget ‘http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=AFR|ZA|SF004|PRETORIA’ -O ~/weather.txt`
GetIMAGE=`grep -A2 Currently ~/weather.txt | grep gif | cut -d”\”" -f2`
IMAGE=`wget $GetIMAGE -O ~/a.gif`
WEATHER=`grep Currently: ~/weather.txt | cut -d”<” -f2 | cut -d”>” -f2`
OUTPUT=`echo -e  ‘~~~~~~~~~~~~’ ‘\n’ LOCAL WEATHER ‘\n’ $WEATHER ‘\n’ ‘~~~~~~~~~~~~’ ‘\n’ LOCAL SYSTEM ‘\n’ BATTERY $STATUS $PERCENT% ‘\n’ CPU TEMPERATURE $TEMP ‘\n’ Wireless Access Point: $WIFI ‘~~~~~~~~~~~~’`
notify-send –urgency=critical -i ~/a.gif ‘INFORMATION’ “$OUTPUT”

I left the -x so that you can troubleshoot where it fails. Have fun!

The advantages of using notify-send with a keyboard shortcut instead of conky are apparent. You get a notification over your open windows, and if you move your mouse over it, it fades. You can call your notification on demand, and it fades out of the way after a short while.

No related posts.