14

I have connected to my server via SSH and created a process, but suddenly my internet connection got disconnected. I know that the process is going on, but how can restore previous session to see the progress of it?

T. Zengerink
  • 199
  • 5
  • 13
Arash Mousavi
  • 658
  • 3
  • 8
  • 21

7 Answers7

17

screen is the best to start a command in a terminal that will be kept when you disconnect from it (either on purpose, ctrl+a d, or because connection dies, etc).

HOWEVER you CAN re-attach a running process (ex: the shell of your running session, if still running) to your current terminal (ie, re-attach stdin, stdout, keyboard, but also make the new terminal the controlling terminal for that process) using: reptyr

For the best of both worlds: install screen, start it, and then re-attach that process in that screen sessions with reptyr ^^

Olivier Dulac
  • 1,202
  • 7
  • 14
  • Oliver, how is it possible to re-attach to running process? Could you gimme a hint please? – archer Jan 11 '13 at 15:12
  • @archer : hint : follow the link ^^ (end of my 2nd paragraph) – Olivier Dulac Jan 11 '13 at 17:02
  • @archer : but screen is simpler: once runnign in screen, ctrl+a d : detach from screen (and go home). then to re-attach to screen: "screen -r" (or a more complex way, if things get stuck). To differentiate amongst many screen sessions, give them a name. – Olivier Dulac Jan 11 '13 at 17:06
16

As suggested above, in the future you can use screen to prevent such thing. A short explanation on how to use screen:

  1. screen -S compiling_stuff
  2. Do stuff here. e.g ./configure
  3. If connection is closed for some reason you can reconnect to the server and issue screen -r comp
  4. You can detach the screen session yourself too by pressing CTRL+A+D

Good luck

che
  • 679
  • 1
  • 5
  • 13
Valentin Bajrami
  • 3,870
  • 1
  • 17
  • 25
7

You can't. In the future, you can prevent something like this with using screen or tmux, which where created to allow to reconnect to a session after a disconnect.

Sven
  • 97,248
  • 13
  • 177
  • 225
5

Just thought I would mention http://mosh.mit.edu/

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.

Jon Conley
  • 328
  • 1
  • 7
2

Use screen via ssh to reconnect later to your session.

As far as I know there is no way to reconnect to a closed ssh-session.

Uwe Plonus
  • 574
  • 3
  • 14
1

I can recommend Byobu:

Byobu is an enhancement for the terminal multiplexers GNU Screen or tmux that can be used to provide on screen notification or status as well as tabbed multi window management. It is aimed at providing a better user experience for terminal sessions when connecting to remote servers.

Frank Zafka
  • 121
  • 7
1

I would recommend to use tmux:

You can restore tmux session via tmux attach.

tmux is a software application that can be used to multiplex several virtual consoles, allowing a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from a command line interface, and for separating programs from the Unix shell that started the program.[1] It provides many of the same functions as GNU Screen, but is distributed under a BSD license.

shyoo
  • 11
  • 1