Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Send desktop notifications through ssh X forwarding

last updated: 2023-12-05

Thanks directly to a user at StackExchange! If you have desktop notifications on your ssh server that you wish to display on your local X server, you can accomplish that with a few steps. In the session you intend to forward desktop notifications, you need to make sure the DBUS_* environment variables are set. From Unix.SE:

START="dbus-launch --exit-with-session"
# set dbus for remote SSH connections
if [ -n "$SSH_CLIENT" -a -n "$DISPLAY" ]; then
   machine_id=$(LANGUAGE=C hostnamectl|grep 'Machine ID:'| sed 's/^.*: //')
   x_display=$(echo $DISPLAY|sed 's/^.*:\([0-9]\+\)\(\.[0-9]\+\)*$/\1/')
   dbus_session_file="$HOME/.dbus/session-bus/${machine_id}-${x_display}"
   if [ -r "$dbus_session_file" ]; then
      export $(grep '^DBUS.*=' "$dbus_session_file")
      # check if PID still running, if not launch dbus
      ps $DBUS_SESSION_BUS_PID | tail -1 | grep dbus-daemon 1> /dev/null 2>&1
      [ "$?" != "0" ] && export $(${START}) 1> /dev/null 2>&1
   else
      export $(${START}) 1> /dev/null 2>&1
   fi
fi

I set this ~/bin/forward-notifications.sh, but the author places it directly in his ~/.bashrc. If this snippet is in a separate script the way I use it, you need to dot-source it.

. forward-notifications.sh

But you still also need at least one .service file that handles org.freedesktop.Notifications. I use Xfce on my X server system, so I just installed its notification library.

$ apt-file search .service | grep -i notif
xfce4-notifyd: /usr/lib/systemd/user/xfce4-notifyd.service
$ sudo apt-get install xfce4-notifyd

And then your notify-send or zenity --notification commands will work!

Comments