Tuesday, June 22, 2010

Resume your remote SSH sessions

How many of us already suffered from a network failure while working remotely over SSH? I'd bet most of us, and isn't that frustating that you just lose all the unsaved changes? It's gone and there's no way you can get back to your session.



Although this is the very common situation, fortunately there is a very simple way you can work over a persistent SSH session.
All you have to do is to create a session holder on the server, which you can do with an utility like "screen" [http://www.gnu.org/software/screen/]

Screnn works server side so it's completely independent on the client software you use for the remote connection.

The steps are just:
  1. Remote login (normally) to your server, from windows, linux, whatever client...
  2. run "screen" (make sure it's installed on the server)
You are now working on a server session. To test it, put something to run, e.g. "top" and close the window.
"Oh God, is my session dead?" - not really. Login again and type "screen -ls". You will hopefully see your sessions, like,

There are screens on:

2477.pts-0.server1 (Detached)
2522.pts-0.server1 (Detached)
2 Sockets in /var/run/screen/S-root.


To reconnect to one of them use:

screen -r 2477.pts-0.server1

There are several options to contol the server session,. Below is a short list of the most important ones:
  • Ctrl a c - Creates a new screen session so that you can use more than one screen session at once.
  • Ctrl a n - Switches to the next screen session (if you use more than one).
  • Ctrl a p - Switches to the previous screen session (if you use more than one).
  • Ctrl a d - Detaches a screen session (without killing the processes in it - they continue).

To close a screen session where all tasks are finished you can type Ctrl-D