Skip to content

Instantly share code, notes, and snippets.

@martiell
Created March 4, 2013 18:57
Show Gist options
  • Save martiell/5084510 to your computer and use it in GitHub Desktop.
Save martiell/5084510 to your computer and use it in GitHub Desktop.
Faster alternative to vncserver script
#!/bin/sh
# Output PID of process listening on given port
pid_of_port() {
netstat -ntpl 2>/dev/null | \
PORT=$1 awk 'substr($4, match($4, "[0-9]*$")) == ENVIRON["PORT"] {
print substr($7, 0, match($7, "/") - 1)}'
}
success() {
local p=$(pid_of_port $(( 5900 + $next )))
[ "$p" = "$pid" ] && return 0 || return 1
}
vnc_dir="$HOME/.vnc"
log=$(mktemp --tmpdir="$vnc_dir")
startVNC() {
Xvnc ":$next" \
-depth 24 \
-rfbauth "$HOME/.vnc/passwd" \
> "$log" \
2>&1 &
pid=$!
}
next=0
startVNC
while ! success; do
if [ -z "$(pgrep -P $$)" ]; then
next=$(( next + 1 ))
startVNC
fi
done
prefix="$vnc_dir/$(hostname):$next"
mv "$log" "$prefix.log"
echo $pid > "$prefix.pid"
export DISPLAY=:$next
xsetroot -solid darkgrey
x-window-manager >/dev/null 2>&1 &
echo :$next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment