How To Use Screen Command on Linux Systems

undefined

In this article, we will show you how to install and use screen command on Linux systems(RPM Family “Redhat / CentOS  / Scientific Linux / Fedora” and Debian Family “Debian / Ubuntu”). Screen is recommended with Linux servers that do not have a GUI to share sessions and run commands that will take long time without fearing from connections interruptions.

Screen is a full-screen software program that can be used to multiplexes a physical console between several processes (typically interactive shells). It offers a user to open several separate terminal instances inside a one single terminal window manager.

The screen application is very useful, if you are dealing with multiple programs from a command line interface and for separating programs from the terminal shell. It also allows you to share your sessions with others users and detach/attach terminal sessions.

Advantages of Screen:

  • Use multiple shell windows from a single SSH session.
  • Keep a shell active even through network disruptions.
  • Disconnect and re-connect to a shell sessions from multiple locations.
  • Run a long running process without maintaining an active shell session.

Part 1: Installing Linux Screen Command:

You may or may not find the screen command installed by default on your Linux systems “Redhat / Debian”. This depends on the type of your Linux installation “Minimal installation , Server installation , Server with GUI, etc…”. But for sure, All Minimal Linux installation methods do not have screen command installed on it, so the first step we will do is installing the screen command on both CentOS and Debian Linux systems:

  • On Redhat / CentOS Linux Systems:

To install screen command run the following yum command:

# yum -y install screen
  • On Debian / Ubuntu Linux Systems:

To install screen command run the following apt-get command:

# apt-get -y install screen

At this point we are 100% sure that screen exists on our Linux system, Now let’s start using it.

Part 2: Using Linux Screen Command:

  • Control Command

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. this control commands mainly used in the screen session itself “you must attached to a screen session”.

  • Starting Linux Screen

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

# screen

You are now inside of a window within screen. This functions just like a normal shell except for a few special characters. Here you can run any command that may take long time safely without worrying about connections interruptions.

  • Detaching From Screen

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

You can detach from the screen using “Ctrl-a” “d”. Then this will drop you into your shell. All screen windows are still there and you can re-attach to them later. Once you execute the “Ctrl-a” “d”, you will see the following message:

# screen 
[detached from 18710.pts-0.localhost]
#
Hint:
1.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:

# screen -r

This will re-attach to your screen.

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

# screen -r
There are several suitable screens on:
 19468.pts-0.localhost (Detached)
 19439.pts-0.localhost (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
If you get this, just specify the screen you want.
# screen -r 19468.pts-0.localhost
OR use the screen PID
# screen -r 19468
  • List Running Screens

To list the running screens on your Linux machine, use any of the following commands:

# screen -ls
There are screens on:
 19468.pts-0.localhost (Detached)
 19439.pts-0.localhost (Detached)
2 Sockets in /var/run/screen/S-root.

OR

# screen -r
There are several suitable screens on:
 19468.pts-0.localhost (Detached)
 19439.pts-0.localhost (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

The second one either re-attach to your current screen “If you have only one screen running” or List all running screens “if you have more than one screen running”.

  • Using Multiple Screens

When you need more than 1 screen to do your job, is it possible? Yes it is. You can run multiple screen window at the same time. There are 2 (two) ways to do it.

First, you can detach the first screen and the run another screen on the real terminal. Second, you do nested screen.

  • Switching between screens

When you do nested screen, you can switch between screen using command “Ctrl-A” and “n“. It will be move to the next screen. When you need to go to the previous screen, just press “Ctrl-A” and “p“.

To create a new screen window, just press “Ctrl-A” and “c“.

  • Logging Your Screen Output

As a consultant, I find it important to keep track of what I do to someone’s server. Fortunately, screen makes this easy.

Using “Ctrl-a” “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” “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” “_”.

  • Locking Your Screen Session

If you need to step away from your computer for a minute, you can lock your screen session using “Ctrl-a” “x”.  This will require a password to access the session again.

Screen used by root <root> on localhost.
Password:
  • Stopping Screen

When you are done with your work, I recommend you stop the session instead of saving it for later.  To stop screen you can usually just type exit from your screen shell. This will close that screen window.  You have to close all screen windows to terminate the session.

You should get a message about screen being terminated once you close all windows.

[screen is terminating]

Alternatively, you can use “Ctrl-a” “k”.  You should get a message if you want to kill the screen.

Summary

In this article, we have explained the installation and usage of screen command on Linux systems, we showed you how to create, detach, reattach, and list  screens. Also how to create multiple windows within one screen “a nested screens”. Finally, every Sysadmins must be familiar with the using of screen command.

I hope this article is good enough for you.
See you in other articles.

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

Your email address will not be published. Required fields are marked *