Emmanuel Hernandez

May 25, 2024
Intermediate
Short

Mastering the Ubuntu Screen Command: A Comprehensive Guide

Learn how to use the screen command in Ubuntu to manage multiple terminal sessions. This guide covers creating, detaching, reattaching, and terminating screen sessions efficiently.

Introduction to the screen command

The screen command in Linux, particularly Ubuntu, is a powerful terminal multiplexer. It allows you to create multiple terminal instances (or "windows") within a single terminal session. This is incredibly useful for running long processes, maintaining sessions after a disconnect, or simply organizing your workspace.

Why use screen?

  • Persistent Sessions: Keep your programs running even if you get disconnected from your SSH session or close your terminal window.
  • Multitasking: Manage multiple tasks or commands simultaneously in different screen windows without opening multiple terminal emulators.
  • Session Sharing: (Advanced) Share your terminal session with other users.

Installing screen

In most Ubuntu distributions, screen comes pre-installed. If not, you can easily install it using:

sudo apt update
sudo apt install screen

1. Creating a Screen Session

Starting a new screen session is straightforward.

Basic Creation

Simply type screen in your terminal and press Enter:

screen

This will start a new screen session and you'll be presented with a new shell prompt. It might look like your old terminal, but you're now inside a screen session.

Creating a Named Session

It's highly recommended to name your sessions, especially if you plan to run multiple. This makes them easier to identify and manage.

screen -S my_session_name

Replace my_session_name with a descriptive name (e.g., update_server, data_processing).

2. Detaching From and Reattaching to a Screen Session

One of the primary benefits of screen is the ability to detach from a session (leaving processes running in the background) and reattach later.

Detaching from a Screen Session

Once inside a screen session, you can detach from it by pressing:

Ctrl+a (Control key and 'a' key simultaneously), then release, and then press d (for detach).

Your terminal will return to the original shell prompt, and you'll see a message like [detached from pid.session_name]. The processes you started within the screen session will continue to run.

Listing Active Screen Sessions

To see a list of your current screen sessions (whether attached or detached), use:

screen -ls

Or:

screen -list

The output will look something like this:

There are screens on:
        12345.my_session_name   (Detached)
        67890.another_session   (Attached)
1 Sockets in /run/screen/S-username.

This shows the session ID (e.g., 12345), the session name (if provided), and its status.

Reattaching to a Screen Session

To reattach to a detached session, you can use its ID or name.

  • Reattaching by ID or Name:

    screen -r 12345
    

    Or, if you named your session:

    screen -r my_session_name
    
  • If a session is marked as (Attached) but you know it's not (e.g., due to a broken connection):

    You can force detach it and reattach with:

    screen -d -r session_name_or_id
    

    Or, more forcefully:

    screen -D -RR session_name_or_id
    

    This command first tries to detach and resume. If that fails, it creates a new session if one doesn't exist, or reattaches if it does, detaching any existing client first.

3. Terminating a Screen Session

When you're finished with a screen session and all the processes within it, you can terminate it.

From Inside the Session

The simplest way to terminate a screen session is to exit all shells running within it. If you're at the shell prompt inside the screen session, simply type:

exit

If there are multiple windows within the screen, you'll need to exit each one. Once the last window is closed, the screen session will terminate, and you'll see a message like [screen is terminating].

From Outside the Session (Killing a Specific Session)

If you want to terminate a specific detached session from your main terminal, you can use the -X option along with the session ID or name and the quit command.

screen -X -S session_id_or_name quit

For example:

screen -X -S 12345 quit

Or:

screen -X -S my_session_name quit

This will kill the specified screen session and any processes running within it.

Other Useful screen Tips

  • Scrolling: Inside a screen session, Ctrl+a then Esc enters copy/scrollback mode. You can then use arrow keys, PageUp/PageDown, Home/End to navigate. Press Esc again to exit this mode.
  • Help: Inside a screen session, Ctrl+a then ? displays a list of key bindings.
  • Multiple Windows: screen allows creating multiple windows within a single session (Ctrl+a then c to create, Ctrl+a then n for next, Ctrl+a then p for previous, Ctrl+a then " for a list).

Conclusion

The screen command is an indispensable tool for anyone working extensively on the Linux command line, especially on remote servers. By mastering how to create, detach, reattach, and terminate sessions, you can significantly improve your productivity and ensure your important tasks run uninterrupted.

Start using screen today to take control of your terminal sessions!","rewrite":false}}}