Skip to content

Instantly share code, notes, and snippets.

@kalos
Created February 8, 2024 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalos/b04aaf93bcbed0e2740c9f3dbf260025 to your computer and use it in GitHub Desktop.
Save kalos/b04aaf93bcbed0e2740c9f3dbf260025 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Send desktop notification (Xorg & Wayland support)
#
# Example:
# notify.sh -u critical "resticprofile backup is too old!"
# detect the user id of the user logged in
_EUID=$(loginctl | head -n2 | tail -1 | sed -e 's/^[ \t]*//' | cut -f2 -d' ')
# if var il already set, skip export
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
if [ -S /run/user/$_EUID/bus ]; then
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$_EUID/bus"
else
SESSION=$(loginctl -p Display show-user "$LOGNAME" | cut -d= -f2)
[ -z "$SESSION" ] && exit
LEADER=$(loginctl -p Leader show-session "$SESSION" | cut -d= -f2)
[ -z $LEADER ] && exit
OLDEST=$(pgrep -o -P $LEADER)
[ -z $OLDEST ] && exit
export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$OLDEST/environ)
[ -z "$DBUS_SESSION_BUS_ADDRESS" ] && exit
fi
fi
# use sudo with UID to send notification (configure sudo properly)
sudo -u '#'$_EUID DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS} notify-send "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment