This tutorial will provide an overview of the basics of Linux Screen, a powerful terminal multiplexer that allows you to create, manage, and navigate multiple terminal sessions within a single window. You'll learn how to start, detach, and reattach to Screen sessions, as well as explore practical applications of this versatile tool.
Skills Graph
%%%%{init: {'theme':'neutral'}}%%%%
flowchart RL
linux(("Linux")) -.-> linux/ProcessManagementandControlGroup(["Process Management and Control"])
linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"])
linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"])
linux/BasicSystemCommandsGroup -.-> linux/help("Command Assistance")
linux/BasicSystemCommandsGroup -.-> linux/exit("Shell Exiting")
linux/ProcessManagementandControlGroup -.-> linux/bg_running("Background Running")
linux/ProcessManagementandControlGroup -.-> linux/bg_process("Background Management")
linux/SystemInformationandMonitoringGroup -.-> linux/ps("Process Displaying")
linux/SystemInformationandMonitoringGroup -.-> linux/top("Task Displaying")
subgraph Lab Skills
linux/help -.-> lab-415333{{"How to terminate a Linux screen session"}}
linux/exit -.-> lab-415333{{"How to terminate a Linux screen session"}}
linux/bg_running -.-> lab-415333{{"How to terminate a Linux screen session"}}
linux/bg_process -.-> lab-415333{{"How to terminate a Linux screen session"}}
linux/ps -.-> lab-415333{{"How to terminate a Linux screen session"}}
linux/top -.-> lab-415333{{"How to terminate a Linux screen session"}}
end
Understanding the Basics of Linux Screen
Linux Screen is a powerful terminal multiplexer that allows you to create, manage, and navigate multiple terminal sessions within a single window. It provides a way to run multiple commands or applications simultaneously, keep them running in the background, and switch between them seamlessly.
One of the primary use cases for Linux Screen is to enable persistent sessions. This means that you can start a long-running process, detach from the session, and later reattach to it, even from a different location or device. This is particularly useful for tasks such as remote server management, running automated scripts, or maintaining long-running processes that should not be interrupted.
Another key feature of Linux Screen is its ability to support shared sessions. This allows multiple users to connect to the same Screen session, enabling collaboration and remote assistance. This can be especially useful in scenarios where multiple team members need to work on the same project or troubleshoot an issue together.
To demonstrate the basic usage of Linux Screen, let's start a new Screen session. Open your terminal and type the following command:
screen
You will now be inside a new Screen session. You can tell you are in a Screen session because the terminal might look slightly different, or you might see a welcome message.
Now, let's run a simple command inside this Screen session. We will use the top command to see running processes.
top
You should see the top output displaying system processes.
top - HH:MM:SS up X days, HH:MM, X users, load average: X.XX, X.XX, X.XX
Tasks: XXX total, X running, XXX sleeping, X stopped, X zombie
%Cpu(s): X.X us, X.X sy, X.X ni, XX.X id, X.X wa, X.X hi, X.X si, X.X st
MiB Mem : XXXX.X total, XXXX.X free, XXX.X used, XXXX.X buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. XXXX.X avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
XXXX labex 20 0 XXXXXX XXXXX XXXXX S X.X X.X X:XX.XX top
XXXX labex 20 0 XXXXXX XXXXX XXXXX S X.X X.X X:XX.XX zsh
...
To exit the top command, press q.
Now, let's detach from this Screen session. This will leave the session running in the background. To detach, press Ctrl+A followed by d.
Ctrl+A, d
You should see a message indicating that you have detached from the Screen session.
[detached from ...]
You are now back in your original terminal session. The Screen session with the top command (which you exited) is still running in the background.
Listing and Reattaching to Screen Sessions
Now that you have detached from a Screen session, let's learn how to list the running sessions and reattach to one.
To list all running Screen sessions, use the screen -ls command in your terminal.
screen -ls
You should see output similar to this, showing your detached session:
There is a screen on:
XXXXX.pts-X.labex (Detached)
X Sockets in /run/screen/S-labex.
The output shows the session ID (e.g., XXXXX.pts-X.labex) and its status (Detached).
To reattach to the most recently detached Screen session, use the screen -r command.
screen -r
You should now be back inside your Screen session. Since you exited top in the previous step, you will likely see a standard terminal prompt within the Screen session.
If you have multiple Screen sessions running, you can reattach to a specific session by providing its session ID after the screen -r command. For example, if the session ID is 12345, you would use:
screen -r 12345
For this lab, since you likely only have one session, screen -r is sufficient.
Now that you are back in the Screen session, let's exit it properly. You can do this by typing exit within the Screen session.
exit
This will terminate the Screen session and return you to your original terminal.
You can verify that the session is closed by listing the Screen sessions again:
screen -ls
You should see output indicating that there are no Screen sessions running:
No Sockets in /run/screen/S-labex.
Terminating a Screen Session
In the previous step, you learned how to exit a Screen session from within the session using the exit command. However, there might be times when you need to terminate a Screen session from outside the session, for example, if a session is unresponsive or you simply want to close it without reattaching.
To terminate a specific Screen session from your original terminal, you can use the screen -X -S [session_id] quit command. You will need the session ID, which you can get from screen -ls.
First, let's start a new Screen session so we have one to terminate.
screen
Now, detach from this session using Ctrl+A, d.
Ctrl+A, d
List the running sessions to get the session ID.
screen -ls
Note down the session ID from the output (e.g., XXXXX.pts-X.labex).
Now, use the screen -X -S [session_id] quit command to terminate the session. Replace [session_id] with the actual ID you noted.
screen -X -S XXXXX.pts-X.labex quit
You should not see any output if the command is successful.
To confirm that the session has been terminated, list the Screen sessions again.
screen -ls
You should see output indicating that there are no Screen sessions running.
No Sockets in /run/screen/S-labex.
This method is useful for forcefully terminating a Screen session when you cannot access it directly.
Summary
Linux Screen is a valuable tool for managing and maintaining persistent terminal sessions. By understanding how to create, detach, list, reattach, and terminate Screen sessions, you can run long-running processes, collaborate with team members, and ensure that your critical tasks continue running even when you're not directly connected to the terminal. This tutorial has covered the essential concepts and practical examples of using Linux Screen, equipping you with the knowledge to enhance your productivity and streamline your terminal-based workflows.