Skip to content

Instantly share code, notes, and snippets.

@p120ph37
Created September 24, 2011 07:12
Show Gist options
  • Save p120ph37/1239069 to your computer and use it in GitHub Desktop.
Save p120ph37/1239069 to your computer and use it in GitHub Desktop.
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/')
echo XAUTHORITY is $XAUTHORITY for DISPLAY $DISPLAY
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