e@9: #!/bin/bash e@9: # Based on: http://www.richud.com/wiki/Ubuntu_Fluxbox_GUI_with_x11vnc_and_Xvfb e@9: e@9: readonly G_LOG_I='[INFO]' e@9: readonly G_LOG_W='[WARN]' e@9: readonly G_LOG_E='[ERROR]' e@9: e@9: main() { e@9: launch_xvfb e@9: launch_window_manager e@9: run_vnc_server e@9: } e@9: e@9: launch_xvfb() { e@9: # Set defaults if the user did not specify envs. e@9: export DISPLAY=${XVFB_DISPLAY:-:1} e@9: local screen=${XVFB_SCREEN:-0} e@9: local resolution=${XVFB_RESOLUTION:-1280x1024x24} e@9: local timeout=${XVFB_TIMEOUT:-5} e@9: e@9: # Start and wait for either Xvfb to be fully up or we hit the timeout. e@9: Xvfb ${DISPLAY} -screen ${screen} ${resolution} & e@9: local loopCount=0 e@9: until xdpyinfo -display ${DISPLAY} > /dev/null 2>&1 e@9: do e@9: loopCount=$((loopCount+1)) e@9: sleep 1 e@9: if [ ${loopCount} -gt ${timeout} ] e@9: then e@9: echo "${G_LOG_E} xvfb failed to start." e@9: exit 1 e@9: fi e@9: done e@9: } e@9: e@9: launch_window_manager() { e@9: local timeout=${XVFB_TIMEOUT:-5} e@9: e@9: # Start and wait for either fluxbox to be fully up or we hit the timeout. e@9: fluxbox & e@9: local loopCount=0 e@9: until wmctrl -m > /dev/null 2>&1 e@9: do e@9: loopCount=$((loopCount+1)) e@9: sleep 1 e@9: if [ ${loopCount} -gt ${timeout} ] e@9: then e@9: echo "${G_LOG_E} fluxbox failed to start." e@9: exit 1 e@9: fi e@9: done e@9: } e@9: e@9: run_vnc_server() { e@9: local passwordArgument='-nopw' e@9: e@9: if [ -n "${VNC_SERVER_PASSWORD}" ] e@9: then e@9: local passwordFilePath="${HOME}/x11vnc.pass" e@9: if ! x11vnc -storepasswd "${VNC_SERVER_PASSWORD}" "${passwordFilePath}" e@9: then e@9: echo "${G_LOG_E} Failed to store x11vnc password." e@9: exit 1 e@9: fi e@9: passwordArgument=-"-rfbauth ${passwordFilePath}" e@9: echo "${G_LOG_I} The VNC server will ask for a password." e@9: else e@9: echo "${G_LOG_W} The VNC server will NOT ask for a password." e@9: fi e@9: e@9: x11vnc -display ${DISPLAY} -forever ${passwordArgument} & e@9: wait $! e@9: } e@9: e@9: control_c() { e@9: echo "" e@9: exit e@9: } e@9: e@9: trap control_c SIGINT SIGTERM SIGHUP e@9: e@9: main e@9: e@9: exit