Guide to Linux screen Command


Introduction

When working on a Linux terminal, you may sometimes need to keep several processes running at same time. However, if you close terminal or accidentally disconnect from a remote session, processes will terminate, and you may lose all your progress. This is where Linux screen command comes in handy.

Screen is a powerful command-line utility that allows you to create and manage multiple terminal sessions within a single shell window or over SSH connections. It is a handy tool for managing long-running processes or multiple commands, even if you are not physically connected to server.

In this article, we will discuss basics of Linux screen command, its usage, and some useful tips and tricks to manage your terminal sessions effectively.

Installing Screen

Most modern Linux distributions come with screen pre-installed. However, if it is not already installed on your system, you can install it using package manager for your distribution. For instance, on Ubuntu, you can install it with following command −

sudo apt-get install screen

After installation, you can start using screen command.

Creating a New Screen Session

To create a new screen session, simply type following command −

screen

This will start a new shell session within a screen session. Now, you can run any commands you want, just like you would in a normal terminal.

Detaching from a Screen Session

To detach from a screen session, you can press keyboard shortcut "Ctrl+A" followed by "d". This will detach screen session and return you to shell prompt. All processes that were running inside screen session will continue to run in background.

Re-Attaching to a Screen Session

To re-attach to a screen session, you can use following command −

screen -r

This will re-attach you to last active screen session. If there are multiple screen sessions running, you can specify session ID to re-attach to a specific screen session.

screen -r <session_id>

Listing Active Screen Sessions

To list all active screen sessions, you can use following command −

screen -ls

This will show you a list of all active screen sessions and their corresponding session IDs.

There are screens on:
   1168.pts-0.ip-10-0-0-45       (Detached)
   1323.pts-0.ip-10-0-0-45       (Detached)
   1343.pts-0.ip-10-0-0-45       (Detached)
3 Sockets in /run/screen/S-ubuntu.

Killing a Screen Session

To kill a screen session, you can use following command −

screen -X -S <session_id> quit

This will send "quit" command to specified screen session, which will terminate all processes running within it.

Splitting Screen

One of most useful features of screen command is ability to split terminal window into multiple regions. This can be especially helpful when working on multiple tasks simultaneously.

To split screen horizontally, you can use following command −

Ctrl+A, |

This will split terminal window into two horizontal regions. Now, you can run different commands in each region.

To split screen vertically, you can use following command −

Ctrl+A, S

This will split terminal window into two vertical regions.

Navigating Between Regions

To navigate between different regions, you can use following commands −

Ctrl+A, Tab

This will switch between different regions.

Ctrl+A, <arrow_key>

Resizing Regions

You can resize different regions of screen by using following commands −

Ctrl+A, :resize +<number>

This will increase size of current region by specified number of lines.

Ctrl+A, :resize -<number>

This will decrease size of current region by specified number of lines.

Copying and Pasting Text

You can copy and paste text between different regions of screen using following commands −

Ctrl+A, [

This will enter copy mode. You can use arrow keys to navigate to text you want to copy.

Spacebar

This will start selecting text.

Enter

This will copy selected text.

To paste copied text, you can use following command −

Ctrl+A, ]

This will paste copied text at cursor position.

Naming a Screen Session

You can give a name to a screen session for easier identification using following command −

screen -S <session_name>

This will create a new screen session with specified name.

screen -r <session_name>

This will re-attach to screen session with specified name.

Advanced Usage of Screen Command

In addition to basic usage of screen command, there are several advanced features that can make your experience even better.

Creating a New Session with a Specific Configuration

You can create a new screen session with a specific configuration by using a configuration file. This can be especially helpful if you want to customize environment of your new screen session.

To create a new screen session with a specific configuration, you can use following command −

screen -c <config_file>

This will create a new screen session with configuration specified in configuration file.

Customizing Status Line

You can customize status line of screen command to display information about current screen session. This can include session name, time, and number of windows.

To customize status line, you can use following command −

Ctrl+A, :hardstatus string '<status_line>'

This will set status line to specified string.

Creating and Switching Between Windows

In addition to splitting terminal window into multiple regions, you can create and switch between multiple windows within same screen session.

To create a new window, you can use following command −

Ctrl+A, c

This will create a new window within same screen session.

To switch between different windows, you can use following commands −

Ctrl+A, n

This will switch to next window.

Ctrl+A, p

This will switch to previous window.

Ctrl+A, <window_number>

This will switch to specified window number.

Sending Commands to Other Windows

You can send commands to other windows within same screen session without switching to them.

To send a command to another window, you can use following command −

Ctrl+A, :number <window_number> stuff '<command>
'

This will send specified command to specified window.

Monitoring Activity in Other Windows

You can monitor activity in other windows within same screen session without switching to them.

To monitor activity in another window, you can use following command −

Ctrl+A, :monitor <window_number>

This will start monitoring activity in specified window.

To stop monitoring activity in a window, you can use following command −

Ctrl+A, :monitor off

This will stop monitoring activity in current window.

Conclusion

The screen command is a powerful utility that can make your life easier when working on a Linux terminal. It allows you to create and manage multiple terminal sessions within a single shell window or over SSH connections. With ability to split terminal window into multiple regions, copy and paste text, and navigate between different regions, screen command can help you manage long-running processes or multiple commands, even if you are not physically connected to server.

In this article, we have discussed basics of Linux screen command, its usage, and some useful tips and tricks to manage your terminal sessions effectively. We hope this guide will help you become more productive and efficient when working on a Linux terminal.

Updated on: 23-Mar-2023

258 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements