Have you ever faced a situation where you perform a long-running task on a remote machine, and suddenly your connection drops, the SSH session is terminated, and your work is lost? Well, it has happened to all of us at some point. Luckily, a utility called screen allows us to Keep the sessions without failure. This blog explores the features and commands of the screen utility in Linux, providing a comprehensive guide on how to enhance multitasking and terminal management efficiently.


Introduction

In the world of Linux, efficiency and multitasking are key to productivity. GNU Screen, a powerful terminal multiplexer, allows users to manage multiple terminal sessions within a single window. With the screen, you can start a session, create multiple virtual terminals, and keep processes running in the background—even if you get disconnected. This makes it an essential tool for system administrators, developers, and anyone who frequently works with remote servers.

In this blog, we’ll explore the features, benefits, and practical usage of the screen command to help you streamline your workflow and enhance your command-line experience.


Requirements

  • A user account with sudo privileges
  • You should have access to a Linux or Unix-based system (local or remote) with a shell interface.
  • Common distributions: CentOS, Ubuntu, Debian, Fedora, etc.


How to Install Screen on Linux

The screen package is pre-installed on most Linux Operating Systems (OS) nowadays. You can check if it is installed on your system by typing:

screen -v

Screen version 4.09.00 (GNU) 30-Jan-22

If the screen utility is not already installed on your system, you can quickly install it using your Linux distribution’s package manager.

  • Install Linux Screen on Ubuntu and Debian

sudo apt update

sudo apt install screen

  • Install Linux Screen on Redhat

sudo yum install screen -y (OR) $ sudo dnf install screen -y


Accessing Help and Keybindings on screen

Ctrl + a and ? – Displays a help menu with a list of all the keybindings inside the screen.

(OR)

we can use the man screen in the terminal to check the list of queries.


How to Use Linux Screen

Below you will find the most common Screen commands with examples of how to use them.

Starting Linux Screen

To launch Linux Screen and start a screen session, run the command:

screen


Getting Started with Screen: Creating and Listing Sessions

While working in Screen, it is recommended to name each session. This helps you keep track of instances if you have multiple screen sessions running.

1. To launch and name a new session, we can use the command:

screen -S session_name

For instance, to create a session named test, we can run:

screen -S test

2. List All Active Screen Sessions

screen -ls This will display a list of sessions, showing their names and statuses.

  • Attached: The session is currently active and connected to a terminal.
  • Detached: The session is running in the background and can be reattached.

Detaching and Reattaching Screen

1. To detach from the screen and leave the window running in the background, use the keystroke:

Ctrl + a and d

The command leaves the process working in Screen and exits the interface. It is the equivalent of minimizing a window in a graphical operating system.

2. To reattach to a running Screen session, run the following command in your main system Terminal:

screen -r

You don’t need to enter the session ID if you only have one Screen instance. If you have more than one, you’ll need to specify which session ID you want to reconnect to.

Each screen session has a different ID and you can see the session ID list with the command using screen -ls.

Note: Replace ID-name with the actual Screen session ID or name.

screen -r 219594 (OR) screen -r screen1


COMMAND-LINE OPTIONS

  • screen -RR: If one or more detached screen sessions exist, it attaches to the most recently used session.
  • screen -D: Detach a session from its current terminal forcefully.
  • screen -X -S 12345 quit: Used to terminate a specific screen session using ID i.e. 12345 in Linux.
  • screen -S future -X stuff ‘echo Hello, Screen!\n’: Delivers the command “echo Hello, Screen!” to the screen session named future, which executes and displays the Hello, Screen! message.

Working with Linux Screen

Once you launch Screen, the application creates a window with a shell inside of the screen session. Add, switch, and manage windows using command keystrokes.

The most commonly used keystrokes include:

  1. echo $STY: using this command we can check the session name inside the screen.

2. Ctrl + a and A: In default, each window is named bash for our understanding we can rename the the window name by using this.

3. Ctrl + a and S: Split the screen horizontally, with the current window on top.

4. Ctrl + a and |: Split the screen vertically, with the current window on the left.


5. Ctrl + a and Ctrl + a: Switch between current and previously active windows.


For this see the above screenshot with the screen window name “0 sun”. And if you note the below screenshot with the name “1 bash”.


6. Ctrl + a and Q: Quit all other windows except the current one. For example, if we have 2 or more terminals opened during that period we need to close all windows except the current one.


7. Ctrl + a and H – Start or Stop logging the output of the current screen window. By default logfile name is screenlog.0.

To list the log file details we can use the ls screenlog*.

!! Important! Release Ctrl + a before pressing the next key to use Linux Screen shortcuts. The commands are case-sensitive, so ensure you correctly enter the uppercase or lowercase character !!


Use keyboard shortcuts to switch between windows in a session

OPTIONSUSES
Ctrl + a and cOpen a new screen window.
Ctrl + a and CClear the screen.
Ctrl-a + n cycle to the next screen window.
Ctrl-a + pmove to the previous window.
Ctrl-a + ID numberswitch to the specific window. The numbering starts from zero.
Ctrl-aShow the number and title of the current window.
Ctrl-a + \This specific command closes (terminates) the current window or ends the session within the screen.
Ctrl-a + kIt starts the copy mode and copies the text entered inside the screen window.
Ctrl-a + [Destroy the current window. Which mainly kills the window we are currently working in.
Ctrl-a + ]It paste the copied text.
Ctrl-a + NIt starts the copy mode and copies the text entered inside the screen window.


View and Select From a List of All Active Windows

Instead of switching back and forth between multiple Screen windows, press Ctrl-a + “ to list all of them. Navigate the list using the arrow keys and press Enter to access the highlighted one.


Locking and Adding Passwords to Screen

To lock the screen, use the shortcut:

Ctrl + a and x

The default lock screen mechanism asks for your Linux password to unlock the screen.


Set a Password for the Specific Screen Session

If you want to set a password for an already running screen session:

Open the screen session.

1. Enter command mode:

Ctrl+a :

2. Type the following command, replacing the password with your desired plaintext password:

password <PASSWORD>

3. After typing the password it will make you set the password for the screen session by entering it,

New screen password:123

4. For the 2 times, we need to retype to confirm which is correct or not.

Retype new password:123


Customizing Screen

Like many Linux applications, Screen uses a customizable configuration file. Find the system-wide configuration file at /etc/screenrc. The user’s configuration file is located at ~/.screenrc.

To edit, open the file:

sudo nano /etc/screenrc


Most settings can be toggled by removing the comment (#) sign at the beginning of the line.


For example:

1. we need to analyze transactions over a few hours in one of our MySQL servers. To do this, we can use the following Percona Toolkit command to capture data in a file

pt-query-digest –processlist h=localhost u=root –ask-pass -S /home/rajseharann/MYSQL/mysql/data/mysql8.sock –interval 0.01 –run-time 30m –output slow log > process_qry_Dec17.log

  • pt-query-digest: A tool to analyze MySQL queries and show performance insights.
  • –processlist: Tells the tool to look at the current queries running on the MySQL server.
  • h=localhost: Connects to the MySQL server on the local machine.
  • u=root: Uses the root user to access the server.
  • –ask-pass: Prompts you to enter the MySQL password.
  • -S /home/rajseharann/MYSQL/mysql/data/mysql8.sock: Specifies the socket file to connect to MySQL, useful when connecting locally.
  • -interval 0.01: Sets how often the tool checks for running queries (every 0.01 seconds).
  • –run-time 30m: Runs the tool for 30 minutes to capture data.
  • –output slow_log: Saves the query analysis results to a file named slow_log.
  • >: It redirects the command output to a file instead of displaying it on the screen.
  • process_qry_Dec17.log: Stores the final output in the log file process_qry_Dec17.log for later review.

Even though we specify a 30-minute run time in the command, the file size increases with each transaction captured during that period.


we can view the file using less process_qry_Dec17.log or cat process_qry_Dec17.log then it will display the content of the processlist inside the log file in the screenshot below.


Why do we need to choose this option?

pt-query-digest –processlist is great for checking queries in real-time and doesn’t need any setup or depend on slow query logs. It’s a useful tool for quickly fixing problems.

Slow query log analysis is better for looking at past performance to find out which queries took longer and to help improve performance over time.


Why We Should Run pt-query-digest –processlist in a Screen Session?

If you’re running the above command for an extended period, say more than 3 hours, to capture transactions during peak times, executing it directly on the local terminal may cause issues. If you accidentally close the terminal session, the command will be interrupted and stopped. To avoid this, you can run the command inside a screen session.

Using the screen ensures that the command will continue running in the background even if the terminal window is closed. This makes it safer to execute long-running tasks without the risk of termination.


Conclusion

The screen command is a versatile tool for multitasking in a terminal, ensuring process continuity, and improving efficiency. Start experimenting with screens today to simplify your workflow. In the coming days, we will be sharing more blogs on exciting topics to enhance your Linux and database management skills—stay tuned!!!


References


https://www.hostinger.in/tutorials/how-to-install-and-use-linux-screen: GNU Screen Unleashed: Take Full Control of Your Terminal
https://phoenixnap.com/kb/how-to-use-linux-screen-with-commands#ftoc-heading-3: GNU Screen Unleashed: Take Full Control of Your Terminal
https://linuxize.com/post/how-to-use-linux-screen/: GNU Screen Unleashed: Take Full Control of Your Terminal
https://www.gnu.org/software/screen/manual/screen.html#index-acladd: GNU Screen Unleashed: Take Full Control of Your Terminal

Discover more from Genexdbs

Subscribe now to keep reading and get access to the full archive.

Continue reading