Linux Pathfinder: Linux Howtos and Tutorials

Linux Screen Command Examples


Linux Screen Command Examples

Screen is a powerful tool in Linux command line interface. Screen command offers a user to open multiple terminal instances from a single ssh session. It offers you to keep a shell active even a network disruption occurs. Linux screen allows you to detach and reattach to a session from multiple locations. It also allows you to share your screen sessions with other users and runs a long running process without disruption.

Installing Screen

Linux screen command already installs on system. You can find screen command in /usr/bin/screen on most RHEL & CentOS distributions. You can find this out from which command in command line.

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

If screen command is not install on server, you can install from redhat package manager (RPM) or with package file as show below:

[root@linuxpathfinder ~]# yum install screen
Resolving Dependencies
--> Running transaction check
---> Package screen.x86_64 0:4.0.3-16.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================================================
Package                  Arch                     Version                         Repository              Size
====================================================================================================
Installing:
screen                   x86_64                   4.0.3-16.el6                    base                   494 k
Transaction Summary
====================================================================================================
Install       1 Package(s)
Total download size: 494 k
Installed size: 795 k
Is this ok [y/N]: y
Downloading Packages:
screen-4.0.3-16.el6.x86_64.rpm                                                           | 494 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : screen-4.0.3-16.el6.x86_64                                                                   1/1
Verifying  : screen-4.0.3-16.el6.x86_64                                                                   1/1
Installed:
screen.x86_64 0:4.0.3-16.el6
Complete!

Starting Screen Command

As we put all commands on linux server command line, screen command is also started from it:

[root@linuxpathfinder ~]# screen

You will see that you have entered inside of a screen window. Now you can start your work as a normal command line environment. The screen application has its parameters.

Type “Ctrl-a” and “?” without quotes and see all parameters or commands in which guide you how to use screen.

Creating Screen Windows

After entering into screen you just use “Ctrl-a” “c” to create a new window in the default prompt and your old window will be still activated.

If you run any command in newly created screen window and after that switch to another window to do other things. Now you will see previous command in old window will be still running.

Switching Windows

“Ctrl-a” “n” screen command allows you to move forward and back. In the above creating window example to switch on screen window to other screen window, this command “Ctrl-a” “n” switches you to the next window. This command work like a circle around to your first window.

Command with “n” option forward to next window and command with “p” option move to previous window.

Each command in different windows will continue its process until you kill that window.

Detaching From Screen

While using screen command if you need to drop into actual shell. You have a detaching option to exit screen command with “Ctrl-a” “d”. This command has an option to reattach the previous screen. So that we can consider it the powerful part of the screen. For example, if you are using rsync command to copy server data to another server. I would recommend to run rsync command into screen to avoid break the session due to any reason. After detaching the screen your all windows will be still threr and safe.

Reattach Linux Screen

You have an option to reattach the screen. However you detach your connection from the screen or your connection lost due to network or some other reason.

[root@linuxpathfinder ~]# screen -r

Above command reattach to your screen.

If you have multiple screens you may get below message:

[root@linuxpathfinder ~]# screen -r
There are several suitable screens on:
27458.pts-0.linuxpathfinder    (Detached)
27447.pts-0.linuxpathfinder    (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

There are another option to see a single or multiple screens like:

[root@linuxpathfinder ~]# screen -ls
There are screens on:
27832.pts-0.linuxpathfinder    (Detached)
27899.pts-0.linuxpathfinder    (Detached)
2 Sockets in /var/run/screen/S-root.

Here you specify the screen if you have multiple screens.

[root@linuxpathfinder ~]# screen -r  27458.pts-0.linuxpathfinder

Logging Screen Output

As you are a Linux Administrator and manages many Linux servers like backups, configurations, taking logs etc. So sometime needs recording screen command logs. Let’s say you are using screen command activate with logging function, to activate logging function press “Ctrl-a” “h” . Now you do not need to put down even a single command which you did. When you activate logs function a notification tells you at the bottom left side of the screen. It creates logfile name “screenlog.0” and save into your home directory.

Everything save into the logfile you write down in screen window. Press again same command to close screen to running log activity.

Getting Alerts on Screen

Another great feature of screen is to monitor a window for any activity. If you are waiting for output of your activity, something compiling or uploading/downloading large files, you can press “Ctrl-a” “m” to look for output at the bottom of the page of your activity.

Lock Screen Session

Lock the screen also have another feature of screen command specially when you need to step away from your system due to any reason. You just need to press “Ctrl-a” “x” shortcut to lock the screen. When you press the shortcut the output become look like as show below.

Screen used by root <root>.
Password:

Now use your Linux password to unlock it.

Leave/Exit Screen

There are two options to leave or exit the screen. First we detach the screen with “Ctrl-a”  “d” and the second option is to exit command to terminating session or use Ctrl-a”  “k” to kill the screen.

There are still a numerous features of screen command. We only discussed such features which are implement on daily basis.

Share this article:

Asif Khan's picture
I am an experienced Linux System and Network Administrator with having more than 6 years of experience. My primary responsibilities are to keep up and running Linux system environment, backups important data with scripting and configuration management on AWS Linux server environments. My aim is to help people, in finding their path towards a solution to their problems. Well, I believe helping others can good for both parties, to them and to you as well. That day and today is the day that thousands of people seek help from my website to help people find answers to their questions. I am sure that everyone will get all the answers to their questions related to software and operating system, and this website will help you keep your system updated.

Comments

WilliamHefs's picture
Submitted by WilliamHefs on May 20, 2016

Appreciate you sharing, great forum post.Thanks Again. Really Great. Kenekham

Add new comment

Image CAPTCHA

Enter the characters shown in the image.