<div dir="auto"><div>Is the project leadership open to accepting pull requests that would integrate similar functionality into the core product?  Perhaps I should join the Dev list and discuss there...<br><div class="gmail_extra"><br><div class="gmail_quote">On Jan 4, 2018 7:11 PM, &quot;Keith Winstein&quot; &lt;<a href="mailto:keithw@cs.stanford.edu">keithw@cs.stanford.edu</a>&gt; wrote:<br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks for sending this! Very interesting, and makes me wonder whether we should think about long-running migratable sessions in a future version of Mosh.<div><br></div><div><div>Best regards,</div><div>Keith</div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div class="elided-text">On Thu, Dec 28, 2017 at 8:45 PM, Jared Sutton <span dir="ltr">&lt;<a href="mailto:jpsutton@gmail.com" target="_blank">jpsutton@gmail.com</a>&gt;</span> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="elided-text"><div dir="ltr">Hi all,<div><br></div><div>I&#39;ve seen several posts in various places about the Mosh&#39;s lack of ability to re-connect to a session on a mosh server after the mosh client that created it had been terminated.  If you reconnect, even from the same system and to the same user account on the server, you&#39;re greeted with the message &quot;<span style="background-color:rgba(27,31,35,0.05);color:rgb(36,41,46);font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:11.9px">Mosh: You have a detached Mosh session on this server</span>&quot;.  This is extremely frustrating to me because it&#39;s basically just saying... &quot;there&#39;s a session on this server running your stuff, but ¯\_(ツ)_/¯&quot;</div><div><br></div><div>Each time this issue is brought up, the solution suggested is to use something like GNU Screen or tmux to maintain the persistence of your session, and reconnect to that as needed.  Obviously, that&#39;s a good bit of manual effort each time you start a mosh session.  My opinion on the matter is that a purposeful integration should be developed for these tools (or other similar ones) to automate their use with mosh, but that&#39;s a discussion for the dev mailing list I think.</div><div><br></div><div>My purpose for this message was to present my own minimal set of changes to my servers/clients to allow reconnecting to sessions automatically.  These changes were developed for my own use cases, so make of them what you will and feel free to suggest changes if you think of any better ways to do what I&#39;m doing, or just ignore me. ;)</div><div><br></div><div>Here&#39;s the TLDR of what my changes do:</div><div><ul><li>On the client end:</li></ul></div><div><ol><ol><li>Create a wrapper shell script around the &quot;mosh&quot; client executable.</li><li>Add an alias entry into .bashrc or .bash_profile to intercept the use of the &quot;mosh&quot; executable</li><li>The wrapper script creates a token string which should be unique to the client system which is passed as an environment variable to the mosh server upon launching a new session.</li></ol></ol><ul><li>On the server end, add a small block of shell script into .bashrc or .bash_profile which starts/resumes a screen session automatically, but only if the shell was started from mosh_server and if the CLIENT_UUID variable is defined and non-empty.  </li><li>The CLIENT_UUID which is passed from client to server provides a way to tie a session to a client system it was initiated from, meaning that it won&#39;t be automatically resumed if I mosh from another client system (but could be manually, if so desired).</li><li>Client-side requirements: bash, md5sum (coreutils), awk</li><li>Server-side requirements: bash, ps (tested on Linux, can&#39;t guarantee Unix/Mac support to the ps options I used)</li></ul><div>And here&#39;s the nitty-gritty changes:</div></div><div><ul><li>Create a wrapper script somewhere (I put mine in a ~/bin directory which I&#39;ve included on my $PATH).  It should contain the following script code:<br></li></ul></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">#!/bin/bash
</span></span></div></div><div><div><span style="font-family:monospace"><br></span></div></div><div><div><span style="font-family:monospace"># Find the block device associated with the root partition
</span></div></div><div><div><span style="font-family:monospace">ROOT_BLK_DEV=$(readlink -f $(df --output=source -h / | tail -1))
</span></div></div><div><div><span style="font-family:monospace">ROOT_UUID=&quot;&quot;
</span></div></div><div><div><span style="font-family:monospace"><br></span></div></div><div><div><span style="font-family:monospace"># Find the UUID of the root partition&#39;s block device
</span></div></div><div><div><span style="font-family:monospace">for uuid in $(ls /dev/disk/by-uuid/); do
</span></div></div><div><div><span style="font-family:monospace">  BLK_DEV=$(readlink -f /dev/disk/by-uuid/&quot;$uuid&quot;)
</span></div></div><div><div><span style="font-family:monospace">  if [ &quot;$ROOT_BLK_DEV&quot; == &quot;$BLK_DEV&quot; ]; then
</span></div></div><div><div><span style="font-family:monospace">    export ROOT_UUID=&quot;$uuid&quot;
</span></div></div><div><div><span style="font-family:monospace">    break
</span></div></div><div><div><span style="font-family:monospace">  fi
</span></div></div><div><div><span style="font-family:monospace">done
</span></div></div><div><div><span style="font-family:monospace"><br></span></div></div><div><div><span style="font-family:monospace"># If we can&#39;t find the root UUID, hash the user@hostname and use that instead
</span></div></div><div><div><span style="font-family:monospace">if [ &quot;$ROOT_UUID&quot; == &quot;&quot; ]; then
</span></div></div><div><div><span style="font-family:monospace">  hostname=&quot;$(hostname -f)&quot;
</span></div></div><div><div><span style="font-family:monospace">  export ROOT_UUID=$(echo &quot;${USER}@${hostname}&quot; | md5sum | awk &#39;{ print $1 }&#39;)
</span></div></div><div><div><span style="font-family:monospace">fi
</span></div></div><div><div><span style="font-family:monospace"><br></span></div></div><div><div><span style="font-family:monospace"># Call the standard mosh client, sending the UUID to the server, and passing the user&#39;s args
</span></div></div><div><div><span style="font-family:monospace">CLIENT_EXEC=$(which mosh)
</span></div></div><div><div><span style="font-family:monospace">$CLIENT_EXEC --server=&quot;CLIENT_UUID=\&quot;$ROOT_<wbr>UUID\&quot; mosh-server&quot; &quot;$@&quot;</span></div></div></blockquote><div><ul><li>Add the following entry into .bashrc or .bash_profile on the <b>client</b> (obviously, this should be modified to point to the location of the wrapper script you created):</li></ul></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">alias mosh=&quot;/home/user/bin/mosh_clie<wbr>nt.sh&quot;</span></span></div></div></blockquote><div><ul><li>Add the following entry into the .bashrc or .bash_profile on the <b>server</b>:</li></ul></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">PARENT_EXEC=$(ps --no-headers -o command -p $PPID | awk &#39;{ print $1 }&#39;)
</span></span></div></div><div><div><span style="font-family:monospace"><br></span></div></div><div><div><span style="font-family:monospace">if [ &quot;$PARENT_EXEC&quot; == &quot;mosh-server&quot; ]; then
</span></div></div><div><div><span style="font-family:monospace">  if [ &quot;$CLIENT_UUID&quot; != &quot;&quot; ]; then
</span></div></div><div><div><span style="font-family:monospace">    screen -DR &quot;$CLIENT_UUID&quot;
</span></div></div><div><div><span style="font-family:monospace">    exit
</span></div></div><div><div><span style="font-family:monospace">  fi
</span></div></div><div><div><span style="font-family:monospace">fi</span></div></div></blockquote><div><div><span style="font-family:monospace"><br></span></div><div>I was thinking about creating a way to automatically deploy this on servers as I connected to them, but I&#39;m hesitant to do such things, as automated tampering of the .bashrc/.bash_profile files could introduce subtle &quot;I can&#39;t login to my server&quot; bugs.</div><div><br></div><div>Anyway, this effort was quick-and-dirty, and I know it&#39;s probably got some warts, so I&#39;m open to critique if you all are interested.</div><div><br></div><div>Thanks,</div><div>Jared </div></div></div>
<br></div>______________________________<wbr>_________________<br>
mosh-users mailing list<br>
<a href="mailto:mosh-users@mit.edu" target="_blank">mosh-users@mit.edu</a><br>
<a href="http://mailman.mit.edu/mailman/listinfo/mosh-users" rel="noreferrer" target="_blank">http://mailman.mit.edu/mailman<wbr>/listinfo/mosh-users</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div></div></div>