Skip to content

Instantly share code, notes, and snippets.

@hrko
Created February 15, 2023 20:03
Show Gist options
  • Save hrko/c7d0933bd327a3b989140a5cf358ab9e to your computer and use it in GitHub Desktop.
Save hrko/c7d0933bd327a3b989140a5cf358ab9e to your computer and use it in GitHub Desktop.
Small script to get the display started by XRDP. This is useful when you want to launch GUI apps from SSH on the XRDP session.
#!/bin/bash
# find session ids for current user
SESSION_IDS=$(loginctl list-sessions -o json | jq -r '.[] | select(.user == "'"$USER"'") | .session')
# find XRDP sessions
XRDP_SESSION_IDS=()
for item in $SESSION_IDS; do
SERVICE=$(loginctl show-session -P Service "${item}")
if [[ $SERVICE == "xrdp-sesman" ]]; then
XRDP_SESSION_IDS+=("${item}")
fi
done
# find active XRDP sessions
ACTIVE_XRDP_SESSION_IDS=()
for item in "${XRDP_SESSION_IDS[@]}"; do
STATE=$(loginctl show-session -P Active "${item}")
if [[ $STATE == "yes" ]]; then
ACTIVE_XRDP_SESSION_IDS+=("${item}")
fi
done
# if there is no active XRDP session, exit
if [[ ${#ACTIVE_XRDP_SESSION_IDS[@]} -eq 0 ]]; then
echo "ERROR: No active XRDP session found." >&2
exit 1
fi
# if there are multiple active XRDP sessions, exit
if [[ ${#ACTIVE_XRDP_SESSION_IDS[@]} -gt 1 ]]; then
echo "ERROR: Multiple active XRDP sessions found." >&2
exit 1
fi
# get the active XRDP session id
ACTIVE_XRDP_SESSION_ID="${ACTIVE_XRDP_SESSION_IDS[0]}"
echo "INFO: Active XRDP session id: ${ACTIVE_XRDP_SESSION_ID}" >&2
# print export command to eval
DISPLAY=$(loginctl show-session -P Display "${ACTIVE_XRDP_SESSION_ID}")
echo "export DISPLAY=${DISPLAY}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment