Asynchronous notifications on UNIX

notify-send

Output is handled either by an in-built option in your desktop manager, e.g. GNOME has one. If you are not using GNOME or something like that, you have to install a notification-daemon yourself.

pacman -S notification-daemon

You can run it manually like this:

/usr/lib/notification-daemon-1.0/notification-daemon

This will now listen to notifications sent with notify-send

To run this automatically, it is best to add it as a D-Bus service (D-Bus takes care of communication).

Make a service manually, e.g. add org.freedesktop.Notifications.service to /usr/share/dbus-1/services:

[D-BUS Service]
Name=org.freedesktop.Notifications
Exec=/usr/lib/notification-daemon-1.0/notification-daemon

notification-daemon is the default implementation.

Let's check out a nice minimalistic implementation called dunst.

pacman -S dunst

dunst doesn't run without a dunstrc, but you can download a default template from their website; or check out usr/share/dunst/dunstrc

[D-BUS Service]
Name=org.freedesktop.Notifications
Exec=/usr/bin/dunst

For an example script using notify-send, see the following script that notifies when the battery is critically low on power:

#!/bin/bash

# notify-send does not work without the following lines
export DISPLAY=:0.0
export XDG_RUNTIME_DIR=/run/user/$(id -u) 

TOO_LOW=20 # how low is too low?
NOT_CHARGING="0"
POWERSUPPLY="/sys/class/power_supply/AC/online"
ICON="/usr/share/icons/HighContrast/48x48/devices/battery.png"

BATTERY_LEVEL=$(acpi -b | grep -P -o '[0-9]+(?=%)')
STATUS=$(cat $POWERSUPPLY)

if [ $BATTERY_LEVEL -le $TOO_LOW -a $STATUS = $NOT_CHARGING ]
then
    /usr/bin/notify-send -u critical -i "$ICON" -t 3000 "Battery low" "Battery level is ${BATTERY_LEVEL}%!"
fi

Make a cronjob for such a script.