Javatpoint Logo
Javatpoint Logo

Linux Screen Command

We sometimes faced a situation in which we implement a long-running operation to remove the machine and our connection drops suddenly. In this type of case, the SSH session has been terminated and our work is gone.

However, it has happened to each of us at a few points. But, there is a command which is known as the screen that permits us to resume the sessions.

Overview of The Screen Command

In Linux, the screen command facilitates the ability for launching and using more than one shell session through an individual ssh session. The process could be detached through the session. We can reattach this session at a later time if the process begins with the screen command.

Originally, the process that was begun from the screen is managed and run by the screen, if the session is detached. After that, the process can reattach the session and the terminals are there in a way it was left.

GNU screen or simply screen can be defined as the terminal multiplexer. Besides, it means that we can start any session of the screen and after that open several windows (virtual windows) in that session

Many processes executing in screen will proceed to execute if the window is not clear even when we get disconnected.

Syntax Of The Screen Command

Options In The Screen Command

  • -a: It will force every capability into the termcap of every window.
  • -A -[r | R]: It will adapt every window to a new display height and width.
  • -c file: It will read configuration file rather than '.screenrc'.
  • -d (-r): It will detach the elsewhere executing screen.
  • -dmS name: It will begin as: Screen session inside the detached pattern.
  • -D (-r): It will support detaching and logging out of the remote.
  • -D -RR: It will implement whatever is required for getting the session of the screen.
  • -e xy: It will modify the characters of the command.
  • -f: It will help for making the flow control over, -fn = off, -fa = auto.
  • -h lines: It will set the scrollback history buffer size.
  • -i: It will interrupt the result sooner if flow control is active.
  • -l: It will help for making the mode of the login on (update /var/run/utmp), -ln = off.
  • -ls [match]: It will display every attached screen.
  • -L: It will turn on the logging of the output.
  • -m: It will ignore the $STY variable and create a new session of the screen.
  • -O: It will select optimal output instead of vt100 emulation (exact).
  • -p window: It will preselect a named window when it is available.
  • -q: It quite a start-up. If unsuccessful, it exists along with a non-zero return code.
  • -Q: It will help for sending the reply to the stdout of a querying process.
  • -r [session]: It will reattach to the process of a detached screen.
  • -R: It will reattach when possible, otherwise begin the new session of the screen.
  • -S (sockname): It will name the session .sockname rather than ...
  • -t (title): It will set the title (name of the window).
  • -T (term): It uses the term as $TERM for windows instead of the "screen".
  • -U: It tells the screen to use the UTF-8 encoding.
  • -v: It prints the "Screen version 4.06.02 (GNU) 23-Oct-17".
  • -x: It will attach to a non-detached screen. It defines the multi-mode of the display.
  • -X: It runs as the screen command inside the described session.

Options for Shortcut Keys:

  • Ctrl-a + c: This shortcut key creates any new window.
  • Ctrl-a + w: This shortcut key displays a list of every window opened currently.
  • Ctrl-a + AIt renames a current window. This name will occur if we list the windows list opened using Ctrl-a + w.
  • Ctrl-a + p: It goes to a previous window.
  • Ctrl-a + n: This shortcut key goes to the next window.
  • Ctrl-a + Ctrl-a: It backs to the window which is last used.
  • Ctrl-a + k: This shortcut key closes the window (current).
  • Ctrl-a + S: It horizontally splits the window (current). For switching between many windows, we can do Ctrl-a + Tab.
  • Ctrl-a + |: It vertically splits the windows (current).
  • Ctrl-a + d: This shortcut key detaches the session of the screen without restricting it.
  • Ctrl-a + r: It reattached the session of the detached screen.
  • Ctrl-a + [: This shortcut key starts the mode of the copy.
  • Ctrl-a + ]: It pasted the text (copied).

Examples of The Screen Command

1. Check the screen version

On most of the distributions of Linux, the package of the screen command is pre-installed. We can consider if it is already installed on our system by entering:


Linux Screen Command

In case, we do not have a screen package already installed on our system, we can install it easily with the help of the package manager of distribution.

2. Installation of the screen command

We need to go to our terminal and enter the below command for installing the screen command:


Linux Screen Command

If we are using Fedora and CentOS, we can use the following command to install the screen command:

3. screen

This command will begin a new window inside the screen.


Linux Screen Command

4. -S

This command starts a new window inside the screen. It also provides a name for our window. It establishes a session recognized by that specific name. The name could be used for re-attaching the screen at a later time.


Linux Screen Command

5. Beginning named session

The named sessions are very useful if we execute more than one session of the screen. We need to execute the screen command along with the below arguments for creating a named session:


Linux Screen Command
Linux Screen Command

Always, it is a good idea to select a descriptive name of the session.

6. -ls

It is used for displaying the opened screens currently including those executing inside the background. This command will list every attached and detached session of the screen.


Linux Screen Command

7. -d

It is used for detecting the session of a screen so that it could be re-attached later. Also, it can be implemented using the Ctrl-a + d shortcut key.


Linux Screen Command
Linux Screen Command

Here 4774 is the id of the screen we wish to detach.

8. -r

It is used for re-attaching the session of a screen that was previously detached.


Linux Screen Command

9. Customize the screen of Linux

When the screen began, it will read the configuration parameters through the ~/ .screenrc and /etc/screenrc if the file is available. We can change the default settings of the screen based on our preferences with the help of the file, i.e., .screenrc.

10. Switching among screens

When we implement a nested screen, we can switch among screens with the help of the "Ctrl-A" command and "n". It will move to the upcoming screen. We have two go on the previous screen, \ just click "Ctrl-A" and "p".

For creating a new screen window, then just click "Ctrl-A" and "c".

11. Using more than one screen

When we need multiple screens for doing our job, we can execute more than one screen window at a time. We can do it in two ways.

First of all, we can detach the initial screen and execute another screen over the actual terminal. Second, we do a nested screen.

12. Logging whatever we do

Sometimes it's necessary to record what we have done during we are inside the console. For example, we are a Linux administrator who handles various servers of Linux. Using the screen logging, we do not have to write down every single command that we have done. For activating the function of screen logging, we need to click "Ctrl-A" and "H".

Note: We have to be careful while using the 'H' letter because 'h' will just establish a screenshot of the screen inside another file named hardcopy.

At the screen's bottom-left side, there will be an alert that notifies us like: Creating logfile "screenlog.0". We will see the screenlog.0 file inside our home directory.

This advantage appends everything we do during we are inside the screen window. For closing the screen to log executing activity, we need to click "Ctrl-A" and "H" again. Other ways for activating this logging feature, we can include the "-L" parameter during the running screen the first time.

This command would be as follows:

We can use the below command for checking the manual page of the screen command:


Linux Screen Command

We can use the below command for checking the help page of the screen command:


Linux Screen Command





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA