<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>@Lathi.net: Scripting Screen</title>
    <link>http://blog.lathi.net/articles/2008/09/13/scripting-screen</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>On Life, Fatherhood, Christianity, and Computers</description>
    <item>
      <title>Scripting Screen</title>
      <description>&lt;p&gt;I do pretty much all my work using &lt;a href="http://www.google.com/url?sa=U&amp;#38;start=1&amp;#38;q=http://www.gnu.org/software/screen/"&gt;&lt;span class="caps"&gt;GNU&lt;/span&gt; Screen&lt;/a&gt; inside a terminal. I use a different screen session for each project and have pretty much the same window configuration for each session.  After finally getting tired of manually setting up my screen sessions, here&amp;#8217;s how  I managed to script new session setup.&lt;/p&gt;


	&lt;p&gt;Like many I have a development directory where all my projects live.  Inside this &lt;code&gt;devel&lt;/code&gt; dir I have the trunk of each project checked out from version control.  Most of my projects haven&amp;#8217;t been converted to &lt;code&gt;git&lt;/code&gt; yet, so I&amp;#8217;m not sure how different it&amp;#8217;d be compared to &lt;code&gt;svn&lt;/code&gt;.  My screen session is made up of several windows.  Inside the first, I run emacs.  The second is autotest.  The third I run &lt;code&gt;./script/server&lt;/code&gt;.  I also have a window for &lt;code&gt;./script/console&lt;/code&gt; and a database prompt; but the order isn&amp;#8217;t that important.  After those I&amp;#8217;ll have a window or two for misc. bash prompts.&lt;/p&gt;


	&lt;p&gt;One solution to automatically setting all this up is to put screen commands inside your &lt;code&gt;.screenrc&lt;/code&gt; to create and name the windows.  You can even specify what command to run in the windows.  The problem is that when you exit that command for whatever reason, the screen window is killed with it.  That&amp;#8217;s not so handy when you need to restart autotest or your server.  Also, the &lt;code&gt;.screenrc&lt;/code&gt; file doesn&amp;#8217;t allow for any type of scripting for dynamically generating any of your startup information.&lt;/p&gt;


	&lt;p&gt;After digging in the &lt;code&gt;man&lt;/code&gt; page a little bit I found a few screen commands that allow it to be scripted fairly well from outside scripts.  I&amp;#8217;ll show the important commands here.&lt;/p&gt;


&lt;code&gt;
screen -d -m -S &amp;lt;screen session name&amp;gt;
&lt;/code&gt;

	&lt;p&gt;This will create a new screen session detached but with an input/output stream.  The &lt;code&gt;-S&lt;/code&gt; names the screen session to make it easier to refer to it later.&lt;/p&gt;


&lt;code&gt;
screen -X -S &amp;lt;session name&amp;gt; screen -t test 1
&lt;/code&gt;

	&lt;p&gt;This sends a screen command to the screen session with the given name.  In this case, the screen command is to create a new screen window named &amp;#8220;test&amp;#8221; as window 1.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
screen -X -S &amp;lt;session name&amp;gt; -p 1 stuff "cd $DEVEL/$PROJECT; autotest
" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This again sends a screen command to the named session.  In this case we&amp;#8217;re sending the &lt;code&gt;stuff&lt;/code&gt; command directly to window 1.  The &lt;code&gt;stuff&lt;/code&gt; command sends the string into stdin of the running application in that window.  In this case the running app is a bash prompt.  Note the trailing newline inside the string will cause the command &amp;#8220;typed&amp;#8221; at the bash prompt to be run.&lt;/p&gt;


	&lt;p&gt;The only other problem I ran into is that when creating screen windows like this, they didn&amp;#8217;t show up on my hard status line automatically.  My solution was to setup a little for loop and select each window like this:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
for i in 0 1 2 3 4
do
   screen -X -S &amp;lt;session name&amp;gt; select $i
done
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Finally, when all is setup the way you want the last thing to do is join the resulting screen session like this:&lt;/p&gt;


&lt;code&gt;
screen -x &amp;lt;session name&amp;gt; -p 0
&lt;/code&gt;

	&lt;p&gt;The trailing &lt;code&gt;-p 0&lt;/code&gt; says to start in the first window.&lt;/p&gt;


	&lt;p&gt;You can download my resulting script &lt;a href="http://lathi.net/files/start-screen.sh"&gt;here&lt;/a&gt;.  One thing to note is that I have an entry in &lt;code&gt;/etc/services&lt;/code&gt; for each of my projects.  I use this so that when I run &lt;code&gt;./script/server&lt;/code&gt; I always start on the same port for each project.  Also, I&amp;#8217;m making use of &lt;code&gt;bash&lt;/code&gt;&amp;#8217;s &lt;code&gt;select&lt;/code&gt; command for prompting with project (or which already started screen session) to join/start.  It&amp;#8217;s a little clumsy in that &lt;code&gt;select&lt;/code&gt; wants to keep prompting you for input after you&amp;#8217;re done with the screen session; but it&amp;#8217;s a lot better than what I had.&lt;/p&gt;</description>
      <pubDate>Sat, 13 Sep 2008 09:11:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:820ca81c-1dc9-4248-9f67-fb57a21ad284</guid>
      <author>Doug</author>
      <link>http://blog.lathi.net/articles/2008/09/13/scripting-screen</link>
      <category>Programming</category>
      <category>script</category>
      <category>screen</category>
      <category>emacs</category>
    </item>
    <item>
      <title>"Scripting Screen" by Doug</title>
      <description>&lt;p&gt;Fixed the download link&lt;/p&gt;</description>
      <pubDate>Thu, 25 Sep 2008 15:23:28 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:daee0980-0018-4b15-bf54-8cbec23f1e37</guid>
      <link>http://blog.lathi.net/articles/2008/09/13/scripting-screen#comment-427</link>
    </item>
    <item>
      <title>"Scripting Screen" by Ryan Schwartz</title>
      <description>&lt;p&gt;Yeah, the script link is broken, and if you don&amp;#8217;t want to send the trailing newline, you can send a ^M (ctrl+v then hit enter) and it works as well&lt;/p&gt;</description>
      <pubDate>Thu, 25 Sep 2008 14:43:30 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:f5c5eb75-655a-499a-9336-c47ac6bc35a7</guid>
      <link>http://blog.lathi.net/articles/2008/09/13/scripting-screen#comment-426</link>
    </item>
    <item>
      <title>"Scripting Screen" by Jim Weirich</title>
      <description>&lt;p&gt;The &lt;a href="http://lathi.net/files/start-screen.sh" rel="nofollow"&gt;http://lathi.net/files/start-screen.sh&lt;/a&gt; link doesn&amp;#8217;t seem to work.&lt;/p&gt;</description>
      <pubDate>Sat, 13 Sep 2008 13:09:38 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:9bd308b4-c0a4-407e-a1a6-98c25231068b</guid>
      <link>http://blog.lathi.net/articles/2008/09/13/scripting-screen#comment-425</link>
    </item>
  </channel>
</rss>
