Weblog entry #2 for lindenle
#2
Setting $DISPLAY in reconnected screen session
Posted by lindenle on Mon 9 Jul 2007 at 18:59
So you just lost your connection to a remote host in the middle of
editing a file with vi. In a past life you would have sworn a blue
streak and perhaps pounded the keyboard, but you are a power user now so
you just reconnect and re-attach your screen session. In fact you are so
happy that you decide to open xeyes just for laughs...but alas the
$DISPLAY environment variable in your screen session no longer matches
the new $DISPLAY variable set by ssh. You could of course set it by
hand to the correct value or even to the display of the actual client
you are connecting from. However thanks to the bash preexec utility
covered here and download-able here we can automate this process.
The first step is to have bash write out a file that can be read by the
preexec command when we log in over ssh. To do this I added the following
lines to my .bashrc file:
So now the information we need is stored in a dot file which we can tell our preexec command to read. In my last entry we had the preexec command set our screen title to the command that was running:
In this case we will modify the command to also set the DISPLAY
environment variable after setting the window title.
So now let's see if it worked. First I log in check the DISPLAY
variable and start a new screen session.
We see that of course the screen session has the correct DISPLAY right
now. Now I will log in from another window, check the DISPLAY and
detach/re-attach this session to test the method.
Looks like it works. In the future I will never have to set the
DISPLAY variable by hand in my screen windows. The only drawback is
that these changes will only take effect on a new screen
session. Which means you will have to exit all your sessions and start
new ones.
editing a file with vi. In a past life you would have sworn a blue
streak and perhaps pounded the keyboard, but you are a power user now so
you just reconnect and re-attach your screen session. In fact you are so
happy that you decide to open xeyes just for laughs...but alas the
$DISPLAY environment variable in your screen session no longer matches
the new $DISPLAY variable set by ssh. You could of course set it by
hand to the correct value or even to the display of the actual client
you are connecting from. However thanks to the bash preexec utility
covered here and download-able here we can automate this process.
The first step is to have bash write out a file that can be read by the
preexec command when we log in over ssh. To do this I added the following
lines to my .bashrc file:
# check to see if DISPLAY is set
# if so write it to a file
if test -n "$DISPLAY"; then
echo "$DISPLAY" > .display.screen
fi
So now the information we need is stored in a dot file which we can tell our preexec command to read. In my last entry we had the preexec command set our screen title to the command that was running:
function precmd () {
set_screen_title "{@$(hostname -s) $(basename `pwd -P`)}: $1"
}
function preexec () {
set_screen_title "{@$(hostname -s) $(basename `pwd -P`)}: $1"
}
In this case we will modify the command to also set the DISPLAY
environment variable after setting the window title.
function precmd () {
set_screen_title "{@$(hostname -s) $(basename `pwd -P`)}: $1"
export DISPLAY="$(cat ~/.display.screen)"
}
function preexec () {
set_screen_title "{@$(hostname -s) $(basename `pwd -P`)}: $1"
export DISPLAY="$(cat ~/.display.screen)"
}
So now let's see if it worked. First I log in check the DISPLAY
variable and start a new screen session.
{lindenle@localhost lindenle} [19:50:41]
=>> echo $DISPLAY
localhost:42.0
{lindenle@localhost lindenle} [19:50:48]
=>> screen
------screen starts--------
{lindenle@localhost lindenle} [19:51:00]
=>> echo $DISPLAY
localhost:42.0
We see that of course the screen session has the correct DISPLAY right
now. Now I will log in from another window, check the DISPLAY and
detach/re-attach this session to test the method.
{lindenle@localhost lindenle} [19:52:25]
=>> echo $DISPLAY
localhost:50.0
{lindenle@localhost lindenle}
=>> screen -dr
------screen attaches--------
{lindenle@localhost lindenle}
=>> echo $DISPLAY
localhost:50.0
Looks like it works. In the future I will never have to set the
DISPLAY variable by hand in my screen windows. The only drawback is
that these changes will only take effect on a new screen
session. Which means you will have to exit all your sessions and start
new ones.
Comments on this Entry
Nicely done! This tip will come in very handy when I get back to work on Monday as I find myself using ssh many many times a day. Thanks!
[ Parent | Reply to this comment ]
Posted by Anonymous (69.20.xx.xx) on Tue 5 Aug 2008 at 21:53
Works perfectly. This has been one of those lingering issues I never got around to looking into. Thankfully I found your post that solved it for me.
[ Parent | Reply to this comment ]