How to recover a shell after a disconnection

Am doing some work on a remote CentOS 5.6 machine and my network keeps dropping.
Is there a way that I can recover my hung sessions after I reconnect?

EDIT: am doing some updating and installing with yum and am worried this might be a problem if processes keep hanging in the middle of whatever they’re doing.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There is no way, but to prevent this I like using tmux. I start tmux, start the operation and go on my way. If I return and find the connection has been broken, all I have to do is reconnect and type tmux attach.

Here’s an example.

$ tmux
$ make <something big>
......
Connection fails for some reason
Reconect

$ tmux ls
0: 1 windows (created Tue Aug 23 12:39:52 2011) [103x30]

$ tmux attach -t 0
Back in the tmux sesion

Method 2

The recommendations for tmux and screen are both good suggestions. They imply the answer to your question, but don’t actually state it. The answer to the question is: there is no way. If you login via ssh, the shell is ended when the connection drops. The work-around is to login and immediately start a virtual terminal of some kind (this can be done with tools like dtach, abduco, tmux, or screen). When the connection drops, the shell you are in is ended, but you can open a new shell and reconnect to the virtual terminal that is running the shell in which you are actually doing your work.

Method 3

As William said, the short answer is no, there is no way. To prevent this you could use the screen command before you lose connection

Method 4

Someone added reptyr in a comment rather than an answer so I can’t upvote it, but it seems a good answer to the edited question and worked great on CentOS.

see:
https://github.com/nelhage/reptyr
https://www.ostechnix.com/reptyr-move-running-process-new-terminal/

Is there a way that I can recover my hung sessions after I reconnect?

EDIT: am doing some updating and installing with yum and am worried
this might be a problem if processes keep hanging in the middle of
whatever they’re doing.

So same thing, I lost remote connection to a yum update session.
So I searched and found this question…
then reconnected and used screen this time:

  1. reconnect ssh
  2. screen
  3. install reptyr as per the links above
  4. ps -a | grep yum
  5. reptyr psid

and there I am at the recovered prompt which yum was giving some hours earlier.

Technically the answers are all correct, this is NOT a recovery of a hung session, it is reparenting of an orphaned process to the current session.
And very useful it is too…

Method 5

No, you can’t recover a shell after a disconnection. Instead what you can do is ensure the command you were running continues to run after the disconnect.

To achieve this, use “nohup” and “disown” commands, which are usually builtin commands on most shells, i.e. you don’t need to install anything. This only works for non-interactive commands though.

So, the steps would be as follows:

  1. Login to server
  2. Run your command: “nohup sudo yum update &” (this will also log all output to nohup.out file in your current directory)
  3. Run “disown %1”
  4. Feel free to disconnect at this point or wait to be disconnected 🙂

When you come back to the server, simply “tail nohup.out” to see how the command is doing.

Method 6

As many people have suggested screen and tmux, they both support basic functionality, but have distinct specific features, so one can’t say that one is superior to another in all cases. For example, only tmux supports Window-splitting, while only GNU screen can toggle long line wrapping with (Ctrl+a r). See more detailed comparison here.

There also exist tools specifically to fix this problem of ssh:

Autossh is a program to start a copy of ssh and monitor it, restarting it as necessary should it die or stop passing traffic. The idea is from rstunnel.

Mosh is a remote terminal application that allows roaming, supports intermittent connectivity, and provides intelligent local echo and line editing of user keystrokes.
Mosh is a replacement for SSH. It’s more robust and responsive, especially over Wi-Fi, cellular, and long-distance links.

Method 7

This can be done using tmux shell. If you get disconnected the fastest way to reattach a session is:

tmux a #0


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x