Starting X-Windows/New Terminal

My current method of spinning up a new cygwin terminal session leverages mintty. When it kicks off it checks to see if X and my ssh agent are running - if not it starts them.

I have mintty pined to my task bar with the following target set:

C:\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico -

This opens a bash session in my $HOME, executing $HOME/.bash_profile. .bash_profile sources $HOME/.bashrc with the following:

# source the users bashrc if it exists
if [ -f "${HOME}/.bashrc" ] ; then
  export ENV=$HOME/.bashrc
  source "${HOME}/.bashrc"
fi

.bashrc, amongst other things does the following to make sure my ssh-agent is running

export SSH_AUTH_SOCK=/home/$HOME/.ssh/agent.socket

/usr/bin/ssh-add -l > /dev/null 2>&1
rc=$?

if [ $rc -eq 2 ]; then
   echo "NOTICE: starting ssh-pageant..."
   cmd="nohup /usr/bin/ssh-pageant -r -a $SSH_AUTH_SOCK"
   ($cmd > /home/$HOME/.log/start_ssh-pageant.log 2>&1 &)
fi

To make sure X is running it also does the following

# start X if it's not runing, we don't strictly need it but
# its handy having it available if we do want to use it
# used to use pgrep but it is horribly slow

export DISPLAY=:0.0
ps ax | grep XWin  > /dev/null 2>&1
rc=$?
if [ $rc -gt 0 ]; then
   echo "NOTICE: starting XWin..."
   cmd="nohup /bin/XWin -multiwindow -clipboard -trayicon"
   ($cmd > /home/$HOME/.log/start_xwin.log 2>&1 &)
fi

An Older Methodology

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 &