スタック・オーバーフローに参加する
686万人以上のプログラマーが集まるスタック・オーバーフローに参加しませんか?
簡単な登録後、すぐにご利用いただけます。
登録

How do I reattach to a detached mosh session or otherwise get rid of

Mosh: You have a detached Mosh session on this server (mosh [XXXX]).

i.e. what's the mosh equivalent of

screen -D -R

or possibly

screen -wipe

Furthermore, where can this answer be found in documentation?

share|improve this question
up vote 107 down vote accepted

For security reasons, you can not reattach, see https://github.com/keithw/mosh/issues/394

To kill the detached session, use the PID number displayed in that message (that's the 'XXXX' part.) For example, if you see --

Mosh: You have a detached Mosh session on this server (mosh [12345]).

And can run this command:

kill 12345

Also, to close all mosh connections you can:

kill `pidof mosh-server`

Note that if you are currently connected via mosh, this last command will also disconnect you.

share|improve this answer
3  
wonder why Mosh does not just kill it itself instead of issuing warning... – artfulrobot Nov 28 '13 at 19:59
20  
@artfulrobot Because there’s a chance that the detached session belongs to a mosh-client that’s still alive somewhere. Mosh sessions roam and can survive through a suspend/resume (e.g., “Hibernation”) cycle. The problem mosh does not (and cannot easily) solve is detecting that the client machine restarted without gracefully closing the mosh session. – binki Jan 12 '14 at 4:18
6  
Is there a reason not to killall mosh-server instead? Especially since pidof and killall are really the same thing anyway. – Jordan Aug 14 '14 at 12:48
3  
@Jordan: On some systems (Solaris, for example), killall does exactly what it says. – Dennis Williamson Jan 12 '15 at 15:44

To my amazement, I used CRIU (https://criu.org) to checkpoint and restart a mosh client and it worked.

Shocking.

Find your mosh-client's PID:

$ ps -ef | grep mosh

Then, install CRIU according to their instructions.

Then, checkpoint it like this:

$ mkdir checkpoint

$ sudo ./criu dump -D checkpoint -t PID --shell-job

Then, restore it:

$ sudo ./criu restore -D checkpoint --shell-job

And, there it is. Your mosh client is back.

One thing to note, however, is that if your laptop reboots (which is the whole point of what we're trying to protect against), mosh uses a monotonic clock to track time on the client side, which doesn't work across reboots. This will NOT work, however, if your laptop just flat out crashes it won't work because mosh sequence numbers will be out of sync with the version that was checkpointed (the binary will resume, but communication will stop).

In order to fix this, you need to tell mosh to stop doing that and download the mosh source code. Then, edit this file:

cd mosh

vim configure.ac

Then, search for GETTIME and comment out that line.

Then do:

autoreconf # or ./autogen.sh if you've just cloned it for the first time

./configure

make

make install

After that, your CRIU-checkpointed mosh client sessions will survive reboots.

(Obviously you'd need to write something to perform the checkpoints regularly enough to be useful. But, that's an exercise for the reader).

share|improve this answer
    
Be sure to type 'CTRL-L' to refresh the screen's output after restore. – Michael R. Hines Apr 16 '16 at 4:15
    
Out of sheer curiosity, is there a practical benefit of restoring the mosh client session that I am missing? I run tmux on mosh and can just relaunch mosh on the client and reconnect the tmux...is there a benefit of doing this other than its cool (which it really really is!)? – eskhool Nov 16 '16 at 15:52
    
The long answer: github.com/mobile-shell/mosh/issues/394 The short answer is, yes: One should not need a tmux session if the mosh-server daemon is already running on the target server. It not only leaves dangling mosh daemons lying around, but its another set of keystrokes that we shouldn't have to type in the first place. – Michael R. Hines Nov 18 '16 at 5:26

As an addition to Varta's answer, I use the following command to close all mosh connections except the current one:

pgrep mosh-server | grep -v $(ps -o ppid --no-headers $$) | xargs kill

share|improve this answer

I use mosh combined with tmux on the server to ensure my sessions persist in the scenario where the client dies for some reason, e.g. enforced reboot.

P.S. or you could continue to use screen as well, if that's your preference.

share|improve this answer
2  
tmux vs. screen has nothing to do with this. I use tmux as well, and get this same issue with mosh. – Nick Jennings Mar 2 '15 at 19:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.