Ubuntu – Send Notification To All Users

This script has only been tested on Ubuntu based OS but is possible it will work on other builds

#!/bin/sh

#  NotificationAssistant.sh
#
#
#  Created by David Kittell on 11/10/17.
#

notify_all() {
icon=$1 # Review lists in /usr/share/icons/gnome/32x32 or /usr/share/notify-osd/icons
title=$2 # Bolded notification title
msg=$3 # Actual message
time=$4 # Time in milliseconds, 10 seconds = 10000 milliseconds
urgency=$5 # Urgency {low, normal, critical}

if [ -z "${4}" ];
then
time="2000"
fi

if [ -z "${5}" ];
then
urgency="normal"
fi

who | awk '{print $1, $NF}' | tr -d "()" |
while read u d; do
id=$(id -u $u)
#. /run/user/$id/dbus-session
export DBUS_SESSION_BUS_ADDRESS
export DISPLAY=$d
su $u -c "/usr/bin/notify-send -t $time -i '$icon' '$title' '$msg' -u $urgency"
done
}

notify_all "$1" "$2" "$3" "$4" "$5"

You can uses various icon options from /usr/share/icons/gnome/32×32 or /usr/share/notify-osd/icons

I typically put a function like this in /opt so you will need to modify the examples below based on your location

Note with urgency when you mark it as critical the notifications should stay on the screen for 30 seconds and aren’t easily dismiss-able, if you plan to use it time is required otherwise time is optional

Time is in milliseconds so in example 3 we have the message showing for 60 seconds where in example 2 we only have it for 10 seconds

sudo sh /opt/NotificationAssistant.sh "info" "General Information" "Coffee Is Ready"

sudo sh /opt/NotificationAssistant.sh "face-worried" "Urgent Alert" "Coffee Is NOT Ready" "10000" "critical"

With Example 3 notice we can use the HTML br tag to do a simple line break in the body of the notify-send.
Should you have issues with the HTML br tag \n will work

sudo sh /opt/NotificationAssistant.sh "face-embarrassed" "Urgent Alert" "We must reboot your machine, save your progress NOW! <br>Machine will reboot in 10 minutes!" "60000" "critical"

# sudo sh /opt/NotificationAssistant.sh "face-angry" "Urgent Alert" "We must reboot your machine, save your progress NOW! \nMachine will reboot in 10 minutes!" "60000" "critical"

Reference: https://unix.stackexchange.com/questions/2881/show-a-notification-across-all-running-x-displays/230062#230062

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.