15 Screen Command Examples to Manage Linux Terminals

15 Screen Command Examples to Manage Linux Terminals

15 Screen Command Examples to Manage Linux Terminals:

The screen is a terminal multiplexer. Using this, you can run any number of console-based-applications, interactive command shells, course-based applications, etc. You can use screen to keep running program after you accidentally close the terminal, or even after you log out and later resume right wherever you are.

How does screen work?

When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill the current window, view a list of the active windows, copy text between windows, switch between windows, etc.

screen manages a session consisting of one or more windows each containing a shell or other program. Furthermore, screen can divide a terminal display into multiple regions, each displaying the contents of a window. All windows run their programs completely independent of each other. Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the user’s terminal. This is practical to prevent involuntary ssh timeout session

1) Invoke new windows

If  the screen command is not already present on your system, you can install it with the command below

On Debian

# apt-get install screen

On Centos

# yum install screen

You can start screen just by typing screen at the command prompt. This screen command will start a new window within the screen.

# screen

You can start a new window within the screen and also gives a name to the window, for example aloft. It creates a session with identified by that name. The name can be used to reattach at a later stage

# screen -S aloft

Note that you can do all your work as you are in the normal CLI environment.

2) List all the screen processes

As we are able to start new windows within the screen, it is possible to display the currently opened screens including those running in the background. It will list all the existing screen sessions

# screen -ls
There is a screen on:
 10437.aloft (Attached)
1 Socket in /var/run/screen/S-root.

3) Main command of screen

It is possible to operate with screen by using some commands. We will list below the main commands more useful

  • Ctrl-a followed by c: create a new windows
  • Ctrl-a followed by w: display the list of all the windows currently opened
  • Ctrl-a followed by A: rename the current windows. The name will appear when you will list the list of windows opened with Ctrl-a followed by w.
  • Ctrl-a followed by n: go to the next windows
  • Ctrl-a followed by p: go to the previous windows
  • Ctrl-a followed by Ctrl-a: back to the last windows used.
  • Ctrl-a followed by a number from 0 to X: go the windows n° X.
  • Ctrl-a followed by ": choose the windows into which to move on.
  • Ctrl-a followed by k: close the current windows (kill)
  • Ctrl-a followed by S: split the current windows horizontally. To switch between the windows, do Ctrl-a followed by Tab.
  • Ctrl-a followed by |: split the current windows vertically
  • Ctrl-a followed by d: detach a screen session without stopping it
  • Ctrl-a followed by r: reattach a detached screen session
  • Ctrl-a followed by [: start the copy mode
  • Ctrl-a followed by ]: paste the copied texte

4) Show screen parameter

You can list all screen’s parameters for help. To do this, type Ctrl-a followed by the character ?. It will display a list of all the commands

5) Detaching session

The best advantage of the screen command is the possibility to detach a screen session. You can start a screen session on one computer at the office, detach the session from the local terminal, go home, log into our office computer remotely and reattach the screen session to our home computer’s terminal. During the intervening time, all jobs on your office computer have continued to execute. This function is used to prevent the lost of data which occur suddenly during dropped ssh connection.

To good understand what we are talking about, let us take an example. We launch an installation process

Now we will detach the screen with Ctrl-a followed by d. We can check with the command below

# screen -ls
There is a screen on:
 12449.win (Detached)
1 Socket in /var/run/screen/S-root.

It is possible to detach screen with screen -d command followed by the screen id or its name. It means that you will need to open another windows or console to detach the session if the current console have a process in progress. You first need to list current attached screen

# screen -ls
There is a screen on:
 13686.win200 (Attached)
1 Socket in /var/run/screen/S-root.

Now on a new terminal, enter the command below

# screen -d 13686

or you can use the name

# screen -d win200

You will have an output as below which indicates that the screen was detached

[remote detached from 13686.win200]

6) Reattach a session

After you detach the screen, it is possible to reattach it with the command. Normally your screen session is still working so all your processes are not stopped. Before reattaching a session, you need to list the session as above, then choose the session to reattach.

# screen -r 13686

or you can use

# screen -r win200

You can see that after reattaching the session, the windows shows the end of the process which is completed. It means that you can work on ssh and detach a session which you will reattach at your home to continue your work process without losing anything.

7) Create a new interactive shell

If you have some jobs to do and you don’t want to lose your work, it is possible to create a new interactive shell. You can easily have some interactive shells (numbered from 0 to X) open in one terminal window. When you start a new session, you’ll be in shell 0.

To do this, you need to create new windows within existing windows with Ctrl-a followed by c. You will have something like below

You can see screen 3 at the top of the screenshot which indicates that we are on the third screen. At the bottom we can also see 

8) Switch between screen windows

When you have opened more than one screen windows, to switch between each window, press Control-a followed by space or the number of the shell. For example, to switch to shell number 1, press Control-a then 1. For example, we will move on the first screen

The top and the bottom of the screenshot shows that we are on the first screen

9) Split windows

To have a global view of your work, you can need to split your windows instead of having multiple windows. Ctrl-a followed by S or | split your screen horizontally or vertically. It is possible to repeat the operation with no limit. To move another windows, use Ctrl-a followed by Tab.

When the cursor is on the bottom windows, you can create a new window (Ctrl-a followed by c) or call an existing window (Ctrl-a followed by a number)

To close a splitted windows, use Ctrl-a followed by X (Note that it is the uppercase character)

10) Reconnect to a disconnected ssh session

When you first log in,

  • run screen to start a screen session. You get another shell, run commands in that.
# screen -S remote_session
  • When you have finished, detach the screen session then logout to the ssh
[detached from 20995.remote_session]
  • You can list all the screen session first
# screen -ls
There are screens on:
 20995.remote_session (Detached)
 14331.daby (Attached)
 14134.mom (Detached)
3 Sockets in /var/run/screen/S-root.
  • Reconnect to your screen session and continue your work
# screen -d -r remote_ression

The screen command is most used for ssh session because it helps to continue your work after a disconnection without losing the current processes in progress.

11) Scroll up in screen windows

Since screen takes over managing your remote programs, you can’t use your terminal emulator’s scroll features while running screen. You must use the Screen commands to access the scrollback buffer.

  • Use Ctrl-a followed by escape
  • Press the Up and Down arrow keys or the PgUp and PgDn keys to scroll through previous output.

You can see where is my cursor on the screenshot. Each virtual terminal has its own scrollback buffer.

12) See the owner of each screen session

The screen -ls or screen -list commands only show you your own screen sessions even for root. As far as I know that’s as good as it gets for screen itself.

If you want to see which screen sessions have been started by which users look in each users directory in /var/run/screen/

# ls -lR /var/run/screen
/var/run/screen:
total 0
drwx------ 2 patrick patrick 60 Jun 17 20:47 S-patrick
drwx------ 2 root root 100 Jun 17 19:39 S-root

/var/run/screen/S-patrick:
total 0
srwx------ 1 patrick patrick 0 Jun 17 20:47 21941.pat_session

/var/run/screen/S-root:
total 0
srw------- 1 root root 0 Jun 9 16:45 14134.mom
srwx------ 1 root root 0 Jun 9 01:07 14331.daby
srwx------ 1 root root 0 Jun 17 20:46 20995.remote_session

13) Sharing a screen session with one account

You can have two people logged into the same account from remote locations. They can easily share a screen session (so what one types the other sees and vice versa). To do this:

  • Create a named screen session:
# screen -d -m -S share_session
  • Attach to the screen session in your terminal window
# screen -x share_session
  • Have the other person (logged into the same account) also attach to the screen session
# screen -x share_session

you will have exactly the same output displayed on the other location. Everything which will appear on your terminal will also appear on the terminal of the other user.

14) Lock screen session

It is possible to lock screen session without locking you normal session user. It means that you can leave your computer and be sure that nobody will come to unauthorized operations. To do this, use the command Ctrl-a followed by x (Note that it is the lowercase character)

Screen used by root <root> on centos-01.
Password:

15) Terminate screen session

When you have finished and you want to quit screen session, you can use the command Ctrl-a followed by k. You will be prompted for a confirmation

You can also use the exit command. If you want to save your process in progress, You can just detach your screen session before terminating the session.

Conclusion

Screen can monitor a window for activity or inactivity. This is great if you are downloading large files, compiling, or waiting for output. At that time, if needed, you can even start some process from a computer using screen command and detach it when you are done.

Was this Tutorial helpful? Help others share on Facebook, Twitter, and Google Plus!

 
Enjoyed this video?
15 Screen Command Examples to Manage Linux Terminals
"No Thanks. Please Close This Box!"