|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Based on: http://www.richud.com/wiki/Ubuntu_Fluxbox_GUI_with_x11vnc_and_Xvfb |
| 4 | + |
| 5 | +echo "Hello" |
| 6 | + |
| 7 | +readonly G_LOG_I='[INFO]' |
| 8 | +readonly G_LOG_W='[WARN]' |
| 9 | +readonly G_LOG_E='[ERROR]' |
| 10 | + |
| 11 | +main() { |
| 12 | + launch_xvfb |
| 13 | + launch_window_manager |
| 14 | + run_vnc_server |
| 15 | +} |
| 16 | + |
| 17 | +launch_xvfb() { |
| 18 | + # Set defaults if the user did not specify envs. |
| 19 | + export DISPLAY=${XVFB_DISPLAY:-:1} |
| 20 | + local screen=${XVFB_SCREEN:-0} |
| 21 | + local resolution=${XVFB_RESOLUTION:-1024x768x24} |
| 22 | + local timeout=${XVFB_TIMEOUT:-5} |
| 23 | + |
| 24 | + # Start and wait for either Xvfb to be fully up or we hit the timeout. |
| 25 | + Xvfb ${DISPLAY} -screen ${screen} ${resolution} & |
| 26 | + local loopCount=0 |
| 27 | + until xdpyinfo -display ${DISPLAY} > /dev/null 2>&1 |
| 28 | + do |
| 29 | + loopCount=$((loopCount+1)) |
| 30 | + sleep 1 |
| 31 | + if [ ${loopCount} -gt ${timeout} ] |
| 32 | + then |
| 33 | + echo "${G_LOG_E} xvfb failed to start." |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + done |
| 37 | +} |
| 38 | + |
| 39 | +launch_window_manager() { |
| 40 | + local timeout=${XVFB_TIMEOUT:-5} |
| 41 | + |
| 42 | + # Start and wait for either fluxbox to be fully up or we hit the timeout. |
| 43 | + fluxbox & |
| 44 | + local loopCount=0 |
| 45 | + until wmctrl -m > /dev/null 2>&1 |
| 46 | + do |
| 47 | + loopCount=$((loopCount+1)) |
| 48 | + sleep 1 |
| 49 | + if [ ${loopCount} -gt ${timeout} ] |
| 50 | + then |
| 51 | + echo "${G_LOG_E} fluxbox failed to start." |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + done |
| 55 | +} |
| 56 | + |
| 57 | +run_vnc_server() { |
| 58 | + local passwordArgument='-nopw' |
| 59 | + |
| 60 | + if [ -n "${VNC_SERVER_PASSWORD}" ] |
| 61 | + then |
| 62 | + local passwordFilePath="${HOME}/x11vnc.pass" |
| 63 | + if ! x11vnc -storepasswd "${VNC_SERVER_PASSWORD}" "${passwordFilePath}" |
| 64 | + then |
| 65 | + echo "${G_LOG_E} Failed to store x11vnc password." |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | + passwordArgument=-"-rfbauth ${passwordFilePath}" |
| 69 | + echo "${G_LOG_I} The VNC server will ask for a password." |
| 70 | + else |
| 71 | + echo "${G_LOG_W} The VNC server will NOT ask for a password." |
| 72 | + fi |
| 73 | + |
| 74 | + x11vnc -display ${DISPLAY} -forever ${passwordArgument} & |
| 75 | + wait $! |
| 76 | +} |
| 77 | + |
| 78 | +control_c() { |
| 79 | + echo "" |
| 80 | + exit |
| 81 | +} |
| 82 | + |
| 83 | +trap control_c SIGINT SIGTERM SIGHUP |
| 84 | + |
| 85 | +main |
| 86 | + |
| 87 | +exit |
| 88 | + |
0 commit comments