-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview-display.sh
More file actions
executable file
·64 lines (58 loc) · 2.17 KB
/
view-display.sh
File metadata and controls
executable file
·64 lines (58 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# Connect a VNC viewer to the gui-user Xvfb display.
#
# Usage:
# ./view-display.sh # auto-detect running x11vnc
# ./view-display.sh 5902 # connect to specific port
#
# If x11vnc isn't running yet, starts it on the first Xvfb display found.
set -euo pipefail
# Find a VNC viewer
VIEWER=""
for cmd in vncviewer xtigervncviewer; do
if command -v "$cmd" &>/dev/null; then
VIEWER="$cmd"
break
fi
done
if [ -z "$VIEWER" ]; then
echo "No VNC viewer found. Install one:"
echo " sudo apt install tigervnc-viewer"
exit 1
fi
if [ "${1:-}" != "" ]; then
PORT="$1"
else
# Auto-detect: find a running x11vnc and its port
VNC_PID=$(pgrep -f "x11vnc.*-viewonly" 2>/dev/null | head -1 || true)
if [ -n "$VNC_PID" ]; then
# Extract port from /proc/pid/cmdline or listening sockets
PORT=$(ss -tlnp 2>/dev/null | grep "pid=$VNC_PID" | grep -oP ':\K[0-9]+' | head -1 || true)
if [ -z "$PORT" ]; then
PORT=5900
fi
echo "Found running x11vnc (pid=$VNC_PID) on port $PORT"
else
# No x11vnc running — find an Xvfb display and start one.
# Only match actual Xvfb processes (not Xwayland, Xorg, etc.)
XVFB_DISPLAY=$(pgrep -a -x Xvfb 2>/dev/null | grep -oP ' :\K\d+' | head -1 || true)
if [ -z "$XVFB_DISPLAY" ]; then
echo "No Xvfb display found. Launch an app first via the MCP server"
echo "(use vnc=True in launch_app, or run this script after launch)."
exit 1
fi
XVFB_DISPLAY=":$XVFB_DISPLAY"
echo "No x11vnc running. Starting one on Xvfb display $XVFB_DISPLAY..."
x11vnc -display "$XVFB_DISPLAY" -viewonly -shared -nopw -forever -noxdamage -q -autoport 5900 &
sleep 1
VNC_PID=$!
if ! kill -0 "$VNC_PID" 2>/dev/null; then
echo "x11vnc failed to start. Is the Xvfb display still running?"
exit 1
fi
PORT=$(ss -tlnp 2>/dev/null | grep "pid=$VNC_PID" | grep -oP ':\K[0-9]+' | head -1 || echo 5900)
echo "x11vnc started on port $PORT"
fi
fi
echo "Connecting $VIEWER to localhost:$PORT ..."
exec "$VIEWER" "localhost:$PORT"