Screen: The Essential Tool for Managing Remote Terminal Sessions

Linux
Screen: The Essential Tool for Managing Remote Terminal Sessions

July 16, 2023

In the world of system administration and web development, we often find ourselves working with remote servers through SSH connections. However, what happens when we need to run a process that takes a long time and we can't keep our computer on or our connection active? This is where Screen comes in, a powerful utility that allows us to manage multiple terminal sessions and keep processes running even after closing our connection.

What is Screen?

Screen is a command-line utility that acts as a terminal multiplexer. This means it can create and manage multiple terminal sessions within a single window. Each session runs independently, allowing us to switch between them, disconnect, and reconnect without interrupting the running processes.

Main features of Screen

  1. Session persistence: Processes continue running even after closing the SSH connection.
  2. Multiple windows: Allows working on several projects simultaneously within the same SSH session.
  3. Session sharing: Enables multiple users to connect to the same Screen session.
  4. Customization: Offers configuration options to adapt its behavior to our needs.

Basic Screen Tutorial

Installation

In most Linux distributions, Screen comes pre-installed. If not, you can easily install it:

sudo apt-get update
sudo apt-get install screen

Basic commands

  1. Start a new session:

    screen -S session_name
    
  2. Detach from a session (without closing it): Press Ctrl+A, then D

  3. List active sessions:

    screen -ls
    
  4. Reattach to a session:

    screen -r session_name
    
  5. End a session: Inside the session, type exit or press Ctrl+D

Practical case: Downloading a website with wget

Imagine you need to download an entire website using wget, a process that can take hours. Here's how to do it using Screen:

  1. Connect to your remote server via SSH.

  2. Start a new Screen session:

    screen -S site_download
    
  3. Inside the session, run the wget command:

    wget --mirror --convert-links --adjust-extension --page-requisites --no-parent --continue https://example.com/
    
  4. Detach from the session by pressing Ctrl+A, then D.

  5. Now you can close your SSH connection. The download process will continue in the background.

  6. When you want to check the progress, reconnect to the server and the Screen session:

    screen -r site_download
    

This method allows you to start long-duration processes without worrying about keeping your computer on or your SSH connection active.

Conclusion

Screen is an indispensable tool for any system administrator or developer who frequently works with remote servers. Its ability to keep sessions active and manage multiple terminal windows significantly improves efficiency and flexibility in remote work.

Mastering Screen will not only save you time and frustrations but will also allow you to perform complex and long-duration tasks with greater confidence and control. Incorporate Screen into your workflow and take your productivity to the next level!