<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Blog of Ed Plese &#187; x</title>
	<atom:link href="http://www.edplese.com/blog/tag/x/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.edplese.com/blog</link>
	<description>Thoughts on Tech</description>
	<lastBuildDate>Tue, 21 Jun 2011 05:10:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Update $DISPLAY in Screen</title>
		<link>http://www.edplese.com/blog/2009/12/12/update-display-in-screen/</link>
		<comments>http://www.edplese.com/blog/2009/12/12/update-display-in-screen/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 02:37:17 +0000</pubDate>
		<dc:creator>Ed Plese</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[screen]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[x]]></category>

		<guid isPermaLink="false">http://www.edplese.com/blog/?p=115</guid>
		<description><![CDATA[I&#8217;ve been a heavy user of GNU Screen for a number of years.  My typical usage is to start a single screen session and attach to it as I move to different computers, either locally or via SSH.  At times I have a need to run X applications from a shell within screen, but with [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been a heavy user of <a href="http://www.gnu.org/software/screen/">GNU Screen</a> for a number of years.  My typical usage is to start a single <code>screen</code> session and attach to it as I move to different computers, either locally or via SSH.  At times I have a need to run X applications from a shell within <code>screen</code>, but with <code>$DISPLAY</code> set to the value <code>screen</code> was initially run with, this tends to not work after it is detached and attached to from a different location.</p>
<p>A few days ago, I came across <a href="http://sial.org/howto/shell/allsh/"><code>allsh</code></a>, a program that allows commands to be executed in all currently running shells.</p>
<p>I was curious if a similar method might work to fix my <code>$DISPLAY</code> issue.  Ideally, I wanted to be able to attach to the <code>screen</code> session from any location and have <code>$DISPLAY</code> updated in all of the bash subprocesses of <code>screen</code> to properly reflect the desired display.</p>
<p>The following is what I came up with to achieve this.</p>
<p>In <code>~/.profile</code>:</p>
<pre>TRAPUSR2() {
 [ -f ~/.screen-display ] &amp;&amp; . ~/.screen-display
}

trap TRAPUSR2 USR2

# set the $DISPLAY variables if the shell is a child of screen
if [ "`ps -p $PPID -o comm | tail -1`" == "screen" ] ; then
 [ -f ~/.screen-display ] &amp;&amp; . ~/.screen-display
fi</pre>
<p>And in a file named <code>attach</code>, placed somewhere in your <code>$PATH</code>:</p>
<pre>#!/bin/bash

echo "DISPLAY=\"$DISPLAY\"" &gt; ~/.screen-display
echo "SSH_CLIENT=\"$SSH_CLIENT\"" &gt;&gt; ~/.screen-display
echo "SSH_CONNECTION=\"$SSH_CONNECTION\"" &gt;&gt; ~/.screen-display
echo "SSH_TTY=\"$SSH_TTY\"" &gt;&gt; ~/.screen-display
echo "XAUTHORITY=\"$XAUTHORITY\"" &gt;&gt; ~/.screen-display

# detect the pid of screen, but should be smarter if more than one
# instance is running
if [ `screen -ls | awk -F. '/tached/ { print $1 }' | wc -l` != "1" ] ; then
 echo "Unable to detect the desired screen session."
 exit 1
fi
SCREEN_PID=`screen -ls | awk '/tached/ { split($1, a, "."); print a[1] }'`

# find the pids of the shells that are children of screen
BASH_PIDS=`ps -e -o pid,ppid,comm | \
 awk '$2 == var1, $3 ~ /bash/ { print $1 }' var1=$SCREEN_PID`
kill -USR2 $BASH_PIDS

exec screen -d -r</pre>
<p>With this setup, start <code>screen</code> as normal, and then to attach to it from another location, run <code>attach</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edplese.com/blog/2009/12/12/update-display-in-screen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

