Resume Broken or Disconnected SSH Session (Linux)


To totally unlock this section you need to Log-in


Login

That's a common phenomenon that you break or disconnect your existing ssh connection to your host. It could be painful if you were editing a script and didn't save it.

By all means you would like to reconnect and start or resume from the same place where your ssh session broke or disconnected. Linux has a software called screen which let's us resume our broken or disconnected SSH session.

Use Screen to resume broken or disconnected SSH session (Quick Tutorial)

Check whether you have screen installed or not. Type on the command line:

screen -ls

If you have screen installed on your host this will show some output. If you don't have screen installed move to previous step else move to next step. To install screen on Ubuntu host type the following command:

sudo apt-get install screen

When the install is complete goto previous step and make sure screen is installed.

screen -ls

It will show your disconnected SSH sessions. Like as shown in next image.

Resume Broken or Disconnected SSH Session (Linux)

To resume or broken/ disconnected SSH session type screen -R. If you have named the session then you can type the name of session with screen command for eg. screen -R sessioname (a bit of advanced configuration). Following is the command:

screen -R 

You can read further man pages for screen command. This is just very basic tutorial for Resuming Broken or Disconnected SSH Session.

The LONG "screen" tutorials

Chances are that you already have screen on your system. On most Red Hat and CentOS distributions you can find Linux screen in /usr/bin/screen. To see if screen is in your path, you can use the which command:

[root@office ~]# which screen
/usr/bin/screen

If you do not have screen, then you can install it easily from an RPM or the package file for your system. For example, on CentOS you can install screen with yum:

[root@office ~]# yum install screen
...
Complete!

As you probably already have Linux screen or can use an RPM, I am not going to cover the building of screen from source. Lets get on to how to use screen.

Starting Linux Screen

Screen is started from the command line just like any other command:

[root@office ~]# screen

You are now inside of a window within screen. This functions just like a normal shell except for a few special characters.

Control Command

Command: Ctrl+a.

Screen uses the command Ctrl-a that’s the control key and a lowercase “a” as a signal to send commands to screen instead of the shell.

For example, Ctrl-a then “?”. You should now have the screen help page.

Creating Windows

Command: Ctrl+a and then c.

To create a new window, you just use Ctrl+a and then c.

This will create a new window for you with your default prompt. Your old window is still active.

For example, I can be running top and then open a new window to do other things. Top stays running! It is still there. To try this for yourself, start up screen and then run top. (Note: I have truncated some screens to save space.)

  • Start top.
  • Now open a new window with: Ctrl+a and then c.
  • Your top window is still running you just have to switch back to it.

Switching Between Windows

Command: Ctrl+a and then n

Screen allows you to move forward and back. In the example above, you could use Ctrl+a and then n to get back to top. This command switches you to the next window.

The windows work like a carousel and will loop back around to your first window.

You can create several windows and toggle through them with Ctrl+a and then n for the next window or Ctrl+a and then p for the previous window.

Each process will keep running until you kill that window.

Detaching From Screen

Command: Ctrl+a and then d

Detaching is the most powerful part of screen. Screen allows you to detach from a window and reattach later.

You can detach from the window using Ctrl+a and then d.

This will drop you into your shell.

All screen windows are still there and you can re-attach to them later.

This is great when you are using rsync for server migration.

NOTE: If your network connection fails, screen will automatically detach your session!

Reattach to Screen

If your connection drops or you have detached from a screen, you can re-attach by just running:

[jeffh@office ~]$ screen -r

This will re-attach to your screen.

However, if you have multiple screens you may get this:

[jeffh@office ~]$ screen -r
There are several suitable screens on:
31917.pts-5.office      (Detached)
31844.pts-0.office      (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

If you get this, just specify the screen you want.

[jeffh@office ~]$ screen -r  31844.pts-0.office

Logging Your Screen Output

We find it important to keep track of what we do to someone’s server. Fortunately, screen makes this easy.

Using Ctrl+a and then "H", creates a running log of the session.

Screen will keep appending data to the file through multiple sessions. Using the log function is very useful for capturing what you have done, especially if you are making a lot of changes. If something goes awry, you can look back through your logs.

Getting Alerts

Screen can monitor a window for activity or inactivity. This is great if you are downloading large files, compiling, or waiting for output.

If you are waiting for output from a long running program, you can use Ctrl+a and then "M" to look for activity. Screen will then flash an alert at the bottom of the page when output is registered on that screen.

I use this when running a command that takes a long time to return data. I can just fire up the command, switch to another window and not have to keep switching back to check the status.

You can also monitor for inactivity. Why use this?

If you are downloading a large file or compiling a program, you can be notified when there is no more output. This is a great signal to when that job is done. To monitor for silence or no output use Ctrl+a and then "_".