-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathxvfb-run
More file actions
executable file
·155 lines (133 loc) · 3.89 KB
/
xvfb-run
File metadata and controls
executable file
·155 lines (133 loc) · 3.89 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/sh
#
# Robust xvfb-run wrapper for headless / HPC environments
#
# Key features:
# - Clear error logging
# - No use of `tempfile`, `$$`, or explicit XXXXX templates
# - Falls back to running command directly if Xvfb is unavailable
# - Forces headless plotting backends when falling back
#
set -u
PROGNAME="$(basename "$0")"
SERVERNUM=""
AUTO_SERVERNUM=""
ERRORFILE=""
XVFBARGS="-screen 0 1280x1024x24"
XVFB_RUN_TMPDIR=""
AUTHFILE=""
XVFBPID=""
XAUTHORITY_ORIG="${XAUTHORITY-}"
DISPLAY_ORIG="${DISPLAY-}"
usage() {
cat >&2 <<EOF
Usage: ${PROGNAME} [OPTIONS] command [args...]
Options:
-a Automatically choose a free X server number (starting at 99)
-n NUM Use X server number NUM
-e FILE Append logs to FILE (default: stderr only)
-s ARGS Extra arguments for Xvfb
-h Show this help
Behavior:
If Xvfb is not available on PATH, the command is run directly with
headless backends forced (MPLBACKEND=Agg, QT_QPA_PLATFORM=offscreen).
EOF
}
log() {
msg="[$PROGNAME] $*"
echo "$msg" >&2
if [ -n "$ERRORFILE" ]; then
echo "$msg" >>"$ERRORFILE" 2>/dev/null || true
fi
}
die() {
log "ERROR: $*"
exit 1
}
cleanup() {
if [ -n "$XVFBPID" ] && kill -0 "$XVFBPID" 2>/dev/null; then
kill "$XVFBPID" 2>/dev/null || true
wait "$XVFBPID" 2>/dev/null || true
fi
if [ -n "$XVFB_RUN_TMPDIR" ] && [ -d "$XVFB_RUN_TMPDIR" ]; then
rm -rf "$XVFB_RUN_TMPDIR" 2>/dev/null || true
fi
if [ -n "$XAUTHORITY_ORIG" ]; then
export XAUTHORITY="$XAUTHORITY_ORIG"
else
unset XAUTHORITY 2>/dev/null || true
fi
if [ -n "$DISPLAY_ORIG" ]; then
export DISPLAY="$DISPLAY_ORIG"
else
unset DISPLAY 2>/dev/null || true
fi
}
trap cleanup EXIT HUP INT TERM
# Parse options
while getopts "an:e:s:h" opt; do
case "$opt" in
a) AUTO_SERVERNUM=1 ;;
n) SERVERNUM="$OPTARG" ;;
e) ERRORFILE="$OPTARG" ;;
s) XVFBARGS="$XVFBARGS $OPTARG" ;;
h) usage; exit 0 ;;
*) usage; exit 2 ;;
esac
done
shift $((OPTIND - 1))
[ "$#" -ge 1 ] || { usage; exit 2; }
# --- Fallback path: no Xvfb available ---
if ! command -v Xvfb >/dev/null 2>&1; then
log "WARNING: Xvfb not found; running command without virtual display."
export MPLBACKEND="${MPLBACKEND-Agg}"
export QT_QPA_PLATFORM="${QT_QPA_PLATFORM-offscreen}"
exec "$@"
fi
# Required tools if we are using Xvfb
command -v mktemp >/dev/null 2>&1 || die "Required command not found: mktemp"
command -v xauth >/dev/null 2>&1 || die "Required command not found: xauth"
command -v mcookie >/dev/null 2>&1 || die "Required command not found: mcookie"
# Choose server number
if [ -z "$SERVERNUM" ]; then
SERVERNUM=99
if [ -n "$AUTO_SERVERNUM" ]; then
while [ -f "/tmp/.X${SERVERNUM}-lock" ]; do
SERVERNUM=$((SERVERNUM + 1))
[ "$SERVERNUM" -le 200 ] || die "Could not find free X server number"
done
fi
fi
# Temp dir and Xauthority
XVFB_RUN_TMPDIR="$(mktemp -d)" || die "mktemp -d failed"
AUTHFILE="$XVFB_RUN_TMPDIR/Xauthority"
: >"$AUTHFILE" || die "Cannot create $AUTHFILE"
COOKIE="$(mcookie)" || die "mcookie failed"
if ! XAUTHORITY="$AUTHFILE" xauth -q add ":$SERVERNUM" MIT-MAGIC-COOKIE-1 "$COOKIE" \
2>>"${ERRORFILE:-/dev/stderr}"; then
die "xauth failed"
fi
# Start Xvfb
log "Starting Xvfb on :$SERVERNUM"
(
exec Xvfb ":$SERVERNUM" -auth "$AUTHFILE" $XVFBARGS
) >>"${ERRORFILE:-/dev/stderr}" 2>&1 &
XVFBPID=$!
# Wait for socket
i=0
while [ $i -lt 50 ]; do
if [ -S "/tmp/.X11-unix/X$SERVERNUM" ]; then
break
fi
if ! kill -0 "$XVFBPID" 2>/dev/null; then
die "Xvfb died during startup"
fi
i=$((i + 1))
sleep 0.05 2>/dev/null || sleep 1
done
[ -S "/tmp/.X11-unix/X$SERVERNUM" ] || die "Xvfb did not start"
# Run command
export DISPLAY=":$SERVERNUM"
export XAUTHORITY="$AUTHFILE"
log "Running: $*"
exec "$@"