Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Run a command in another session's X-Windows environment, even under gdm3 where the .Xauthority file is hard to find.
# This can be copied+pasted directly into a terminal window
# and the display-authority function will become available.
# usage example:
# display-authority :1 'notify-send "hello, stranger!"'
# Note: this uses sudo access to find the appropriate XAUTHORITY file,
# so you'll need to be a sudoer to use this.
function display-authority() {
export DISPLAY=:${1#:}
PROC=$(sudo fuser /tmp/.X11-unix/X${1#:} 2>/dev/null | sed -re 's/^\s*([0-9]+)\s*$/\1/')
echo Process $PROC owns DISPLAY $DISPLAY
export XAUTHORITY=$(sudo cat /proc/$PROC/cmdline | sed -re 's/^.*-auth\x00([^\x00]+).*$/\1/')
shift 1
echo Executing command: $*
sudo sh -c "$*"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment