Skip to content

Instantly share code, notes, and snippets.

@tullmann
Created November 13, 2015 19:02
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tullmann/476cc71169295d5c3fe6 to your computer and use it in GitHub Desktop.
Save tullmann/476cc71169295d5c3fe6 to your computer and use it in GitHub Desktop.
wait for an X11 server to be ready (good for running under XVFB when testing chrome)
#!/bin/bash
#
# waitForX [<cmd> [<arg> ...]]
#
# Wait for X Server to be ready, then run the given command once X server
# is ready. (Or simply return if no command is provided.)
#
function LOG {
echo $(date -R): $0: $*
}
if [ -z "$DISPLAY" ]; then
LOG "FATAL: No DISPLAY environment variable set. No X."
exit 13
fi
LOG "Waiting for X Server $DISPLAY to be available"
MAX=120 # About 60 seconds
CT=0
while ! xdpyinfo >/dev/null 2>&1; do
sleep 0.50s
CT=$(( CT + 1 ))
if [ "$CT" -ge "$MAX" ]; then
LOG "FATAL: $0: Gave up waiting for X server $DISPLAY"
exit 11
fi
done
LOG "X is available"
if [ -n "$1" ]; then
exec "$@"
fi
#eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment