Starting X-Windows/New Terminal

Cygwin has a number of ways of starting X-Windows. I wanted something that I could use to start X if it isn't running, start my ssh agent if it's not running and begin a new terminal session. This lets me bind a single icon to my Windows quick launch and associate a keyboard shortcut with it.

This uses Cygwin's run command to invoke a bash script. The shortcut has it's target set to:

C:\cygwin64\bin\run.exe -p /usr/bin /usr/bin/bash.exe --login /home/ourusername/bin/xwrap.sh

and it's “Start in:” set to:

C:\cygwin64\home\ourusername

The bash script that does the work:

xwrap.sh
#!/bin/sh
 
# set up some basic environment variables. 
#
# we're going to use a socket for our ssh agent so we don't
# have a bunch of agents running
 
export SSH_AUTH_SOCK=/home/ourusername/.ssh/agent.socket
export SHELL=/bin/bash
export DISPLAY=:0.0
 
# we make use of ssh-pageant which allows or cygwin ssh sessions
# to use putty's pageant - this means we don't have to have seperate
# agents in cygwin and windows - which means we only have to add
# keys once
 
# I've had a few issues in the past where very long agent sessions
# get broken - so we try to start it, we timeout after six seconds
# if we timeout we will kill the currently running version and 
# start a fresh one
 
cmd="/usr/bin/ssh-pageant -r -a $SSH_AUTH_SOCK"
/usr/bin/timeout 6s $cmd
rc=$?
if [ $rc -ge 124 ]; then
   taskkill /f /im ssh-pageant.exe
   $cmd
fi
 
# now we check to see if X is running, if it isn't
# we start it in the background and give it a few 
# seconds to get going
 
rc=`/usr/bin/pgrep XWin`
if [ ${rc:-null} = null ]; then
   /bin/XWin -multiwindow -clipboard -trayicon &
   /usr/bin/sleep 3
fi
 
# now we spin up our new terminal session
 
exec /usr/bin/xfce4-terminal --default-display=:0.0 &