3 # $Id: xvfb-run 2027 2004-11-16 14:54:16Z branden $
5 # This script starts an instance of Xvfb, the "fake" X server, runs a command
6 # with that server available, and kills the X server when done. The return
7 # value of the command becomes the return value of this script.
9 # If anyone is using this to build a Debian package, make sure the package
10 # Build-Depends on xvfb, xbase-clients, and xfonts-base.
19 XVFBARGS
="-screen 0 640x480x8"
20 LISTENTCP
="-nolisten tcp"
23 # Query the terminal to establish a default number of columns to use for
24 # displaying messages to the user. This is used only as a fallback in the event
25 # the COLUMNS variable is not set. ($COLUMNS can react to SIGWINCH while the
26 # script is running, and this cannot, only being calculated once.)
27 DEFCOLUMNS
=$
(stty size
2>/dev
/null |
awk '{print $2}') || true
28 if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev
/null
2>&1; then
32 # Display a message, wrapping lines at the terminal width.
34 echo "$PROGNAME: $*" |
fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
37 # Display an error message.
39 message
"error: $*" >&2
42 # Display a usage message.
45 message
"usage error: $*"
48 Usage: $PROGNAME [OPTION ...] COMMAND
49 Run COMMAND (usually an X client) in a virtual X server environment.
51 -a --auto-servernum try to get a free server number, starting at
53 -e FILE --error-file=FILE file used to store xauth errors and Xvfb
54 output (default: $ERRORFILE)
55 -f FILE --auth-file=FILE file used to store auth cookie
56 (default: ./.Xauthority)
57 -h --help display this usage message and exit
58 -n NUM --server-num=NUM server number to use (default: $SERVERNUM)
59 -l --listen-tcp enable TCP port listening in the X server
60 -p PROTO --xauth-protocol=PROTO X authority protocol name to use
61 (default: xauth command's default)
62 -s ARGS --server-args=ARGS arguments (other than server number and
63 "-nolisten tcp") to pass to the Xvfb server
64 (default: "$XVFBARGS")
65 -w DELAY --wait=DELAY delay in seconds to wait for Xvfb to start
66 before running COMMAND (default: $STARTWAIT)
70 # Find a free server number by looking at .X*-lock files in /tmp.
71 find_free_servernum
() {
72 # Sadly, the "local" keyword is not POSIX. Leave the next line commented in
73 # the hope Debian Policy eventually changes to allow it in /bin/sh scripts
78 while [ -f /tmp
/.X
$i-lock ]; do
86 if [ -e "$AUTHFILE" ]; then
87 XAUTHORITY
=$AUTHFILE xauth remove
":$SERVERNUM" >>"$ERRORFILE" 2>&1
89 if [ -n "$XVFB_RUN_TMPDIR" ]; then
90 if ! rm -r "$XVFB_RUN_TMPDIR"; then
91 error
"problem while cleaning up temporary directory"
97 # Parse the command line.
98 ARGS
=$
(getopt
--options +ae
:f
:hn
:lp:s
:w
: \
99 --long auto-servernum
,error-file
:,auth-file
:,help,server-num
:,listen-tcp
,xauth-protocol
:,server-args
:,wait: \
100 --name "$PROGNAME" -- "$@")
103 if [ $GETOPT_STATUS -ne 0 ]; then
104 error
"internal error; getopt exited with status $GETOPT_STATUS"
112 -a|
--auto-servernum) SERVERNUM
=$
(find_free_servernum
) ;;
113 -e|
--error-file) ERRORFILE
="$2"; shift ;;
114 -f|
--auth-file) AUTHFILE
="$2"; shift ;;
115 -h|
--help) SHOWHELP
="yes" ;;
116 -n|
--server-num) SERVERNUM
="$2"; shift ;;
117 -l|
--listen-tcp) LISTENTCP
="" ;;
118 -p|
--xauth-protocol) XAUTHPROTO
="$2"; shift ;;
119 -s|
--server-args) XVFBARGS
="$2"; shift ;;
120 -w|
--wait) STARTWAIT
="$2"; shift ;;
122 *) error
"internal error; getopt permitted \"$1\" unexpectedly"
129 if [ "$SHOWHELP" ]; then
135 usage
"need a command to run" >&2
139 if ! which xauth
>/dev
/null
; then
140 error
"xauth command not found"
144 # tidy up after ourselves
147 # If the user did not specify an X authorization file to use, set up a temporary
148 # directory to house one.
149 if [ -z "$AUTHFILE" ]; then
150 XVFB_RUN_TMPDIR
="$(mktemp -d -t $PROGNAME.XXXXXX)"
151 AUTHFILE
="$XVFB_RUN_TMPDIR/Xauthority"
156 XAUTHORITY
=$AUTHFILE xauth
source - << EOF >>"$ERRORFILE" 2>&1
157 add :$SERVERNUM $XAUTHPROTO $MCOOKIE
159 XAUTHORITY
=$AUTHFILE Xvfb
":$SERVERNUM" $XVFBARGS $LISTENTCP >>"$ERRORFILE" \
163 if ! kill -0 $XVFBPID 2>/dev
/null
; then
164 echo "Xvfb failed to start" >&2
168 # Start the command and save its exit status.
170 DISPLAY
=:$SERVERNUM XAUTHORITY
=$AUTHFILE "$@" 2>&1
174 # Kill Xvfb now that the command has exited.
177 # Return the executed command's exit status.
180 # vim:set ai et sts=4 sw=4 tw=80: