Using Screen on Linux: create, resume, and close terminal sessions

Using Screen on Linux: create, resume, and close terminal sessions

Screen is a command-line tool on Linux that allows you to manage multiple, persistent terminal sessions. With screen, you can run programs in the background, detach from a session without closing it, and resume it later.

This is useful for long-running or remote tasks, since your processes keep running even if you disconnect from the server. It also allows you to split the screen, create multiple windows, and maintain efficient control over your sessions.

You can also combine the use of screen sessions with sending email via SSH.

Creating a new screen session

To start a new screen session, simply type:

  1. screen

This will start a new session and take you to a shell inside screen.

Create a named session

It is useful to assign a name to the session to identify it more easily:

  1. screen -S session_name

Detaching from a session (detach)

To detach from a screen session without closing it (leaving it running in the background), press:

  1. Ctrl + a + d

This will return you to the original shell, and the screen session will continue running in the background.

List active screen sessions

To view all screen sessions currently running:

  1. screen -ls

The output will look something like:

  1. There is a screen on:
  2. 1234.my_long_process
  3.   (Detached)
  4. 1 Socket in /var/run/screen/S-user.

Resume a screen session

To resume a detached session:

If the session has a name:

  1. screen -r session_name

If the session has no name or there are multiple sessions, list them first with:

  1. screen -ls

Then resume it using the session ID:

  1. screen -r 1234

Terminate a screen session

To end a screen session, you can do it in several ways:

From inside the session

  1. exit
or press:
  1. Ctrl + d

From outside the session

Kill the session by name:
  1. screen -S session_name -X quit
Kill the session by ID:
  1. screen -X -S 1234 quit

Useful commands inside screen

While you are inside a screen session, you can use several commands.

Detach the session:
  1. Ctrl + a + d
View the list of screen commands:
  1. Ctrl + a + ?
Create a new window within screen:
  1. Ctrl + a + c
Switch between windows:
  1. Ctrl + a  + n
Close the current window:
  1. Ctrl + a +  k

Advanced screen usage

Split the screen

You can split the screen to view multiple windows at the same time.

Split horizontally:
  1. Ctrl + a + S
Split vertically:
  1. Ctrl + a + | 
Move between regions:
  1. Ctrl + a + Tab
Close the current region:
  1. Ctrl + a + X

Additional tips

  • Custom configuration: You can customize screen behavior by editing the following file:
  1. ~/.screenrc
Session logging: To log everything that happens in a screen session, you can start recording with:
  1. Ctrl + a + H
This will create a file in the current directory:
  1. screenlog.0
The screen tool is extremely powerful and flexible for managing persistent terminal sessions on Linux. With this tutorial, you should be able to create, resume, and terminate screen sessions, as well as use some of its advanced features to improve your workflow.

    • Related Articles

    • How to delete iDrive e2 files using AWS CLI on Mac

      When you need to delete large amounts of files from an iDrive e2 bucket, doing it through the web interface can take hours or even days. The most efficient solution is to use AWS CLI from your Mac terminal, as it processes multiple files in parallel ...
    • How to Export and Import a MySQL Database Using Command Line

      Having an up-to-date backup of your database is essential to protect your critical information and ensure business continuity. In this tutorial, we will show you how to export a database using the command line. It is important to perform a complete ...
    • JetBackup 5: How to fix the Integrity Check error using CLI/SSH

      When JetBackup performs its automated integrity verification processes, they may sometimes complete partially due to network interruptions, timeouts, or communication issues with the remote storage destination. If you receive notifications indicating ...
    • Clean /tmp on Linux: remove old temporary files with tmpwatch

      The /tmp partition on a Linux server is used to temporarily store files while various tasks are running. It is important to ensure that this partition does not become full, as it can affect system performance and cause unexpected issues. In this ...
    • Basic SSH commands

      FILE MANAGEMENT Copy file cp -a archivo.zip archivocopia.zip Create a 404.html file touch /home/usuario/public_html/404.html Compress directory zip -r archivo.zip /home/usuario/public_html/directorio Extract file unzip archivo.zip View contents of a ...