notify-send in Ubuntu 16 LTS

notify-send notification bubble during the backup process showing the text 'gDrive auto-backup started!'

I recently upgraded to Ubuntu 16 LTS from Ubuntu 14 LTS.
Unlike my previous experiences, this time the process was not that smooth.
When the ‘upgrade’ option resulted in more than half of my manually installed software being removed, I thought it’s rather better to do a fresh install instead.

While the new system looked pretty sharp with most of the things working well, there was one major problem: the suspend feature wasn’t working, thanks to a bug in the kernel related to AMD graphics.
How I solved it is a story for another day.

After exploring the new features for a while (which didn’t take much time), I started setting up the system, installing a lot of software and copying some configuration files from the previous install.

After playing for a while, I decided to backup my files on Google Drive using a small application I created a while back.
Here I hit another road block; notify-send (the app used to create those bubble notifications during the backup process) was simply refusing to run when run from cron.
I remember it happening in Ubuntu 14 too; the reason was that the environment in which cron operates is very minimalist, and notify-send required the presence of a certain environment variable DISPLAY=0:0 to work.
So, I easily fixed this problem by adding the definition of that variable inside the cron script.

But this trick wasn’t working anymore in Ubuntu 16 !

After googling a lot and trying many different hacks, I finally found the solution:

Turns out, apart from the variable DISPLAY, we need 2 more environment variables for notify-send to work:
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-KqjYaobROx
XAUTHORITY=/home/anmol/.Xauthority

But there’s another catch; the value of DBUS_SESSION_BUS_ADDRESS refers to a location in the /tmp directory, which means that this value is temporary and is valid only for a single login session.
So if I simply added this variable to the cron script, it wouldn’t have worked after the next login.

The only way to make it work was to get the value of this variable every time a new session starts.

So, I wrote a small shell script to read these variables’ values and output them to another file which was then sourced in the backup script, thus making them accessible during the notify-send invocation.
To make this shell script run on login automatically, I used Ubuntu’s built-in Startup Applications to schedule it.

TIP: To simulate the environment under which cron operates and test whether a script runs as expected from cron, follow these steps.

Leave a comment