{"id":115,"date":"2009-12-12T20:37:17","date_gmt":"2009-12-13T02:37:17","guid":{"rendered":"http:\/\/www.edplese.com\/blog\/?p=115"},"modified":"2010-04-17T08:54:08","modified_gmt":"2010-04-17T13:54:08","slug":"update-display-in-screen","status":"publish","type":"post","link":"https:\/\/www.edplese.com\/blog\/2009\/12\/12\/update-display-in-screen\/","title":{"rendered":"Update $DISPLAY in Screen"},"content":{"rendered":"<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.\u00a0 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.\u00a0 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>\n<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>\n<p>I was curious if a similar method might work to fix my <code>$DISPLAY<\/code> issue.\u00a0 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>\n<p>The following is what I came up with to achieve this.<\/p>\n<p>In <code>~\/.profile<\/code>:<\/p>\n<pre>TRAPUSR2() {\r\n\u00a0[ -f ~\/.screen-display ] &amp;&amp; . ~\/.screen-display\r\n}\r\n\r\ntrap TRAPUSR2 USR2\r\n\r\n# set the $DISPLAY variables if the shell is a child of screen\r\nif [ \"`ps -p $PPID -o comm | tail -1`\" == \"screen\" ] ; then\r\n\u00a0[ -f ~\/.screen-display ] &amp;&amp; . ~\/.screen-display\r\nfi<\/pre>\n<p>And in a file named <code>attach<\/code>, placed somewhere in your <code>$PATH<\/code>:<\/p>\n<pre>#!\/bin\/bash\r\n\r\necho \"DISPLAY=\\\"$DISPLAY\\\"\" &gt; ~\/.screen-display\r\necho \"SSH_CLIENT=\\\"$SSH_CLIENT\\\"\" &gt;&gt; ~\/.screen-display\r\necho \"SSH_CONNECTION=\\\"$SSH_CONNECTION\\\"\" &gt;&gt; ~\/.screen-display\r\necho \"SSH_TTY=\\\"$SSH_TTY\\\"\" &gt;&gt; ~\/.screen-display\r\necho \"XAUTHORITY=\\\"$XAUTHORITY\\\"\" &gt;&gt; ~\/.screen-display\r\n\r\n# detect the pid of screen, but should be smarter if more than one\r\n# instance is running\r\nif [ `screen -ls | awk -F. '\/tached\/ { print $1 }' | wc -l` != \"1\" ] ; then\r\n\u00a0echo \"Unable to detect the desired screen session.\"\r\n\u00a0exit 1\r\nfi\r\nSCREEN_PID=`screen -ls | awk '\/tached\/ { split($1, a, \".\"); print a[1] }'`\r\n\r\n# find the pids of the shells that are children of screen\r\nBASH_PIDS=`ps -e -o pid,ppid,comm | \\\r\n\u00a0awk '$2 == var1, $3 ~ \/bash\/ { print $1 }' var1=$SCREEN_PID`\r\nkill -USR2 $BASH_PIDS\r\n\r\nexec screen -d -r<\/pre>\n<p>With this setup, start <code>screen<\/code> as normal, and then to attach to it from another location, run <code>attach<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been a heavy user of GNU Screen for a number of years.\u00a0 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.\u00a0 At times I have a need to run X applications from a shell within screen, but with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[10,12,11],"_links":{"self":[{"href":"https:\/\/www.edplese.com\/blog\/wp-json\/wp\/v2\/posts\/115"}],"collection":[{"href":"https:\/\/www.edplese.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.edplese.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.edplese.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edplese.com\/blog\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":11,"href":"https:\/\/www.edplese.com\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":127,"href":"https:\/\/www.edplese.com\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions\/127"}],"wp:attachment":[{"href":"https:\/\/www.edplese.com\/blog\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edplese.com\/blog\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edplese.com\/blog\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}