Skip to content

Instantly share code, notes, and snippets.

@isaaclw
Last active November 27, 2022 15:12
Show Gist options
  • Save isaaclw/84ff57049401f11a38cbf3fb26f008cc to your computer and use it in GitHub Desktop.
Save isaaclw/84ff57049401f11a38cbf3fb26f008cc to your computer and use it in GitHub Desktop.
Load display
# http://stackoverflow.com/a/1162345/971529
load_display() {
if [ -z "$1" ]; then
PID=$(pgrep -n -u $USER 'gnome-session|xfdesktop|xterm|kodi|lxsession|mate-session')
else
PID=$(pgrep -n -u $USER -f "$1")
fi
FALLBACK=$(pgrep -n 'unity-greeter')
if [ -n "$PID" ]; then
export DISPLAY=$(cat /proc/$PID/environ | strings | awk 'BEGIN{FS="=";} $1=="DISPLAY" {print $2; exit}')
echo "DISPLAY set to $DISPLAY"
elif [ -n "$FALLBACK" ]; then
export DISPLAY=$(sudo cat /proc/$FALLBACK/environ | strings | awk 'BEGIN{FS="=";} $1=="DISPLAY" {print $2; exit}')
echo "Using Login Screen. DISPLAY set to $DISPLAY"
else
echo "Could not set DISPLAY"
fi
unset PID
}
# needs to be a function since it loads it into the current environment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment