How to Use the Screen Command in Linux

The screen command is a terminal multiplexer, which essentially means it allows you to run multiple terminal sessions within a single window. Ever needed to manage long-running tasks or keep your sessions alive even after you’ve logged out? That’s where screen comes into play.

The beauty of screen is its ability to detach and reattach sessions, making it incredibly useful for remote work, long-running scripts, or any task that requires you to hop between multiple terminal windows. Commonly associated commands include screen -r to reattach a session, screen -ls to list active sessions, and Ctrl + A, then D to detach a session without terminating it.

So, whether you’re a system administrator juggling various tasks or a developer running multiple processes, screen is a command you’ll want to add to your Linux toolkit. Read on to learn how to harness its full potential.

General syntax for screen command:

screen [OPTIONS] [SESSION NAME]
1. Create a session with specified name
screen -S [file]

The -S option creates a new window within the screen and names it.

Example:

user@hostname:~$ screen -S my_session

After running this command, you’ll be in the new screen session named my_session. You won’t see the session name directly in the terminal, but you can list all active sessions by detaching from the current session (by pressing Ctrl + A followed by D) and then running screen -ls.

Sample output for screen -ls after detaching:

user@hostname:~$ screen -ls
There is a screen on:
        12345.my_session     (Detached)
1 Socket in /var/run/screen/S-user.

In this example, 12345 is the process ID of the screen session, and my_session is the name you gave to the session.

To reattach to this session, you can use:

screen -r 12345

Or if you named your session:

screen -r my_session

This will bring you back to the screen session you detached from.

2. List all screen processes
screen -ls

This option displays all currently open windows, including those running in the background.

Example:

The output will typically look something like this:

There are screens on:
    12345.pts-0.hostname  (Detached)
    67890.pts-1.hostname  (Attached)
2 Sockets in /var/run/screen/S-username.

Here’s a breakdown of the output:

  • 12345.pts-0.hostname and 67890.pts-1.hostname are the identifiers for the screen sessions.
  • 12345 and 67890 are the process IDs (PIDs) of the screen sessions.
  • pts-0 and pts-1 indicate the terminal types.
  • hostname is the name of the host machine where the screen sessions are running.
  • (Detached) or (Attached) indicates the status of the screen session.
  • Detached means that the session is running in the background and not currently attached to any terminal.
  • Attached means that the session is currently being viewed in a terminal window.
  • 2 Sockets in /var/run/screen/S-username indicates the number of screen sessions and the directory where the session sockets are stored. username is the name of the user who owns the sessions.
3. Reattach the Terminal’s session
screen -r [filename]

Use this option to reattach a screen session which was detached in past.

Example:

Let’s say you have a long-running process, like downloading a large file, and you want to keep it running even after you’ve logged out. You can use screen to create a new session and run the process there.

Create a new screen session:

screen -S my_download_session

This will create a new screen session named my_download_session.

Run your long-running process:

wget http://example.com/large-file.zip

Detach from the screen session:

Press Ctrl + A followed by D to detach from the session. The session will continue running in the background.

Log out or close the terminal:

At this point, you can safely log out or close the terminal. The download will continue in the background.

Reattach to the screen session:

Later, when you want to check the progress or reattach to the session, you can use the screen -r command.

screen -r my_download_session

This will reattach you to the screen session named my_download_session, and you can see the progress of your download or any other long-running process.

If you have multiple detached sessions and you’re not sure what the session names are, you can list them with:

screen -ls

This will show you a list of all the detached and attached screen sessions, and you can pick the one you want to reattach to.

4. Detach a session
screen -d [file]

The -d option is used to detach the screen session, allowing it to be reattached later.

Example:

Here’s a step-by-step example to demonstrate how screen -d [filename] can be used:

Step 1: Create a New Screen Session with a Name

First, create a new screen session and give it a name, for example “my_session“:

screen -S my_session

Step 2: Run Some Commands in the Screen Session

After creating the session, you’ll be inside it. You can run some commands like:

ls
echo "Hello, World!"

Step 3: Detach the Screen Session Manually

You can detach from the screen session manually by pressing Ctrl + A followed by D. This will bring you back to your original terminal, but the session “my_session” will still be running in the background.

Step 4: Reattach to the Screen Session

You can reattach to the session using:

screen -r my_session

Step 5: Detach the Screen Session Using -d

Now, let’s say you are in another terminal and you want to detach the session “my_session” without reattaching to it. You can use the -d option like this:

screen -d my_session

This will detach the session “my_session” if it’s currently attached to any terminal, without terminating it. You can later reattach to it using screen -r my_session.

5. Share a screen’s session
screen -X [sharing_session]

This option allows two people to log into the same account from different locations.

Example:

Here’s a simple example to demonstrate how you can use screen -X to send a command to a running screen session:

Step 1: Create a New Screen Session

First, open a terminal and create a new screen session named “my_session“:

screen -S my_session

Step 2: Detach from the Screen Session

You can detach from the screen session by pressing Ctrl + A followed by D.

Step 3: Send a Command to the Screen Session from Another Terminal

Open another terminal window and use the screen -X command to send a command to the “my_session” screen. For example, let’s send the quit command to terminate the session:

screen -S my_session -X quit
This will terminate the "my_session" screen session.
More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail