Skip to content

Instantly share code, notes, and snippets.

@TopperBG
Created April 5, 2019 12:35
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 TopperBG/c235aa3f465c9c98b0c2a46a7682d793 to your computer and use it in GitHub Desktop.
Save TopperBG/c235aa3f465c9c98b0c2a46a7682d793 to your computer and use it in GitHub Desktop.
notify-send-as-root.sh
#!/bin/bash
#
# This script shows how to send a libnotify message
# to a specific user.
#
# It looks for a process that was started by the user and is connected to dbus.
# process to determine DBUS_SESSION_BUS_ADDRESS
USER_DBUS_PROCESS_NAME="gconfd-2"
USER="makubi"
NOTIFY_SEND_BIN="/usr/bin/notify-send"
TITLE="title"
MESSAGE="notify message"
# get pid of user dbus process
DBUS_PID=`ps ax | grep $USER_DBUS_PROCESS_NAME | grep -v grep | awk '{ print $1 }'`
# get DBUS_SESSION_BUS_ADDRESS variable
DBUS_SESSION=`grep -z DBUS_SESSION_BUS_ADDRESS /proc/$DBUS_PID/environ | sed -e s/DBUS_SESSION_BUS_ADDRESS=//`
# send notify
DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION su -c "$NOTIFY_SEND_BIN \"$TITLE\" \"$MESSAGE\"" $USER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment