Skip to content

Instantly share code, notes, and snippets.

@elgalu
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elgalu/bc52952293f11bd30502 to your computer and use it in GitHub Desktop.
Save elgalu/bc52952293f11bd30502 to your computer and use it in GitHub Desktop.
headless_with_vnc4server
#!/bin/bash
unset VNCSERVERARGS
VNCSERVERS=""
[ -f /etc/headless/vncservers.conf ] && . /etc/headless/vncservers.conf
prog=$"VNC server"
start() {
. /lib/lsb/init-functions
REQ_USER=$2
echo -n $"Starting $prog: "
ulimit -S -c 0 >/dev/null 2>&1
RETVAL=0
for display in ${VNCSERVERS}
do
export USER="${display##*:}"
if test -z "${REQ_USER}" -o "${REQ_USER}" == ${USER} ; then
echo -n "${display} "
unset BASH_ENV ENV
DISP="${display%%:*}"
export VNCUSERARGS="${VNCSERVERARGS[${DISP}]}"
su ${USER} -c "cd ~${USER} && [ -f .vnc/passwd ] && vnc4server :${DISP} ${VNCUSERARGS}"
PORT="590${DISP}"
DISPLAY=:0 su user -c "/home/user/bin/vncview localhost:$PORT" &
fi
done
}
stop() {
. /lib/lsb/init-functions
REQ_USER=$2
echo -n $"Shutting down VNCServer: "
for display in ${VNCSERVERS}
do
export USER="${display##*:}"
if test -z "${REQ_USER}" -o "${REQ_USER}" == ${USER} ; then
echo -n "${display} "
unset BASH_ENV ENV
export USER="${display##*:}"
su ${USER} -c "vnc4server -kill :${display%%:*}" >/dev/null 2>&1
fi
done
echo -e "\n"
echo "VNCServer Stopped"
}
case "$1" in
start)
start $@
;;
stop)
stop $@
;;
restart|reload)
stop $@
sleep 3
start $@
;;
condrestart)
if [ -f /var/lock/subsys/headless ]; then
stop $@
sleep 3
start $@
fi
;;
status)
status Xvnc
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
exit 1
esac
*****************************************************
* New headless method on ubuntu by using vnc4server
*****************************************************
* Tested on Ubuntu 14.04 (hardy) with default gnome
sudo apt-get install vnc4server
* Create a shared folder with selenium scripts
sudo mv selenium /
sudo chmod 775 /selenium
sudo chmod g+rwx /selenium/*.sh /selenium/chromedriver
* Create an extra user
sudo adduser --gecos "" --ingroup user user2
* Impersonate user2 to avoid logoff. -x to disable X11 fwd
ssh -x user2@localhost
* Generate initial config files
vnc4server :2
* Stop vnc server to edit files
vnc4server -kill :2
* Comment line `x-window-manager &` and add your selenium start script, e.g.
vim .vnc/xstartup
* selenium start scripts examples
x-terminal-emulator -geometry 120x30+10+10 -ls -title "$VNCDESKTOP Desktop" -e "/selenium/chrome-to-grid-6666.sh" &
x-terminal-emulator -geometry 120x30+10+10 -ls -title "$VNCDESKTOP Desktop" -e "/selenium/firefox-to-grid-6666.sh" &
* Start vnc server with specific screen resolution
vnc4server :2 -geometry 1400x988
* Repeat for as many users as you want
* How to automate the startup process
sudo mkdir -p /etc/headless
sudo vim /etc/headless/vncservers.conf
#content start
VNCSERVERS="2:user2 3:user3 4:user4 5:user5 6:user6 7:user7 8:user8 9:user9"
VNCSERVERARGS[2]="-geometry 1400x988"
VNCSERVERARGS[3]="-geometry 1400x988"
VNCSERVERARGS[4]="-geometry 1400x988"
VNCSERVERARGS[5]="-geometry 1400x988"
VNCSERVERARGS[6]="-geometry 1400x988"
VNCSERVERARGS[7]="-geometry 1400x988"
VNCSERVERARGS[8]="-geometry 1400x988"
VNCSERVERARGS[9]="-geometry 1400x988"
#content end.
sudo vim /etc/init.d/headless
* Copy content from: http://goo.gl/DZBney
sudo chmod +x /etc/init.d/headless
sudo update-rc.d headless defaults 99
sudo service headless stop
sudo service headless start
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment