8

I know these kinds of questions have been asked for years, and the answer to them are often Screen or tmux.

I surely will use screen at beginning if I know I will leave the session for a long time, or the network is too bad to maintain a reliable connection.

The main problem is when I start some session and find it must last long later, or the connection just lost accidentally. In the later case, often when I start another session immediately, I can find the previous processes are not killed at that time, but I just have no way to reconnect to their terminal.

So I wonder if it is possible to prevent normal processes from killed even long time after accidentally disconnected SSH session. And the most important is I can reconnect to their terminals with out start them in Screen in advance.

If not, is is possible to move a already started bare ssh session into a new Screen session for later reconnect?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lyu
  • 588
  • 1
  • 6
  • 8
  • Out of curiousity, why don't you want to invoke screen? – SpliFF Nov 22 '13 at 02:45
  • 2
    @SpliFF usually it's not I don't want to, but just because it's too late as I said in the question (disconnected accidentally or realize I need a screen in a half-way session). BTW, screen also break scrollback buffer of putty and iterm that allows easily scroll by drag the scroll bar, maybe it can be tweaked? But I am just too lazy to fix that. – lyu Nov 22 '13 at 02:58
  • Please consider using [`mosh`](https://mosh.org/): _Remote terminal application that allows roaming, supports intermittent connectivity, and provides intelligent local echo and line editing of user keystrokes._ – Pablo Bianchi Apr 07 '20 at 19:33

3 Answers3

4

I don't believe it's possible without something like screen. Once your pseudo-TTY is lost I'm almost certain it can't be recovered from a different shell (at least not without some narly hacks).

As far as adding an existing process to a new screen I think that is possible. Try the instructions here:

http://monkeypatch.me/blog/move-a-running-process-to-a-new-screen-shell.html

The first thing to do is to suspend the process. In my case, Irssi can be suspended by typing Ctrl + Z.

Secondly, resume the process in background:

$ bg

Now, we will detach the process from its parent (the shell). So, when the parent process will be terminated, the child (Irssi) will be able to continue. For this, we use the disown builtin:

$ disown irssi

Launch a screen session:

$ screen

As we are in a screen session, we will retrieve the irssi process. To do so, we use the reptyr command which take a pid:

$ reptyr

To avoid the tedious pid research, we can use the pgrep command:

$ reptyr $(pgrep irssi)

Now the process is in a screen shell, we can safely detach our session and no longer worry about killing our X server or close our ssh connection.

You'll need reptyr for this.

OPTION 2:

I suspect you may be trying to solve the wrong problem. If your SSH connection is dropping, why not address that? You can set SSH to be incredibly tolerant of timeouts and disconnects by tweaking your connection settings.

On your client, in $HOME/.ssh/config add:

ServerAliveInterval 60
ServerAliveCountMax 5

Now your sessions won't timeout even if the server doesn't respond for 5 minutes.

Community
  • 1
  • 1
SpliFF
  • 38,186
  • 16
  • 91
  • 120
  • I don't really understand the OPTION 2, I know the timeout can be set, but once I lost the connection, is it all gone? Even if the timeout is inf, I still cannot reconnect to it? – lyu Nov 22 '13 at 02:53
  • 1
    Option 2 is about minimising the chance of losing your connection in the first place. Even if your internet goes off for 4 minutes the options above will keep your session going. But yeah, once it's really gone (like a client reboot, accidentally closed terminal, etc) then that's pretty much it. – SpliFF Nov 22 '13 at 03:03
  • Thank you, I got option 2 now. But Option 1 has some problem: – lyu Nov 22 '13 at 04:24
  • Problems: 1. If I leave the old terminal open and do reptyr in a new terminal, the output is bind to the new terminal, but ctrl-z stop working in the new attached process (ctrl-c still works). 2. If the old terminal is closed, when reptyr in a new one, it gives "[-] Timed out waiting for child stop." and stop responding at all. 3. it seems reptyr still not support children processes (so shell script usually not work with reptyr). – lyu Nov 22 '13 at 04:31
  • Ok, I may get problem 2, disown only prevent SIGHUP when the terminal ends, but stdout/in/err still connect to that terminal, so maybe a new terminal must be start and reptyr to that before closing the old one. It makes sense. – lyu Nov 22 '13 at 04:38
  • Did you remember to CTRL-Z (suspend) the running task and background it before disowning it? – SpliFF Nov 22 '13 at 05:34
  • I did, and bg or not makes no difference. – lyu Nov 22 '13 at 06:00
1

Use ssh-tmux instead of tmux:

function ssh-tmux(){
  if ! command -v autossh &> /dev/null; then echo "Install autossh"; fi
  autossh -M 0 $* -t 'byobu || {echo "Install byobu-tmux on server..."} && bash'
}
Sandeep
  • 28,307
  • 3
  • 32
  • 24
-1

I worked on a text file using nano and I got disconnected. After I logged in I saw the nano process from the previous session was still running, but I couldn't switch to that nano instance. So, I killed the nano process and then it created file named filename.save. Which had my changes from the first session.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
volatile
  • 179
  • 8