Help Center

Manage screen session

Screen is a command-line tool in 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 particularly useful for long-running or remote tasks, as your processes continue running even if you disconnect from the server. Additionally, it lets you split the screen, create multiple windows, and efficiently manage your sessions.

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

To start a new screen session, simply type:

screen

This will start a new session and bring you to a shell within screen.

Create a named session

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

screen -S session_name

Detaching from a session

To detach from a screen session without closing it (i.e., leave it running in the background), press:

Ctrl + a + d

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

Listing active screen sessions

To view all currently running screen sessions:

screen -ls

The output will be something like:

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

Resuming a screen session

To resume a detached session:

  • If the session is named:
    screen -r session_name
  • If the session is unnamed or there are multiple sessions:

    First, list the sessions with screen -ls and then resume using the session ID:
    screen -r 1234

Terminating a screen session

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

From inside the session

  • Exit the shell: Simply type exit or press Ctrl + d to close the shell within screen. This will terminate the session.

From outside the session

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

Useful commands within screen

While inside a screen session, you can use various commands. All commands start with Ctrl + a followed by another key.

  • Detach the session:  Ctrl + a + d
  • View the screen command list: Ctrl + a ?
  • Create a new window within screen: Ctrl + a c
  • Switch between windows: Ctrl + a n (next) or p (previous)
  • Close the current window: Ctrl + a k

Advanced use of screen

Splitting the screen

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

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

Scrollback

To scroll up and view the history:

Ctrl + a then [

You can then navigate using the arrow keys or Page Up and Page Down. To exit scroll mode, press Esc.

Additional tips

  • Custom configuration: You can customize the behavior of screen by editing the ~/.screenrc file.
  • Session logging: To log everything happening in a screen session, you can start recording with:Ctrl + a then H

    This will create a screenlog.0 file in the current directory.

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.

Last updated: 14 de octubre de 2024

Estimated reading time: 6 minutes

Did this article help you?
Let's Do It!

Start TODAY for only $2.45/month

Includes a web builder, 30GB of cloud storage, +200 Apps, and professional email.

Sign Up Now