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:
- 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:
- 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:
- 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:
- screen -ls
The output will look something like:
-
There is a screen on:
-
1234.my_long_process
-
(Detached)
-
1 Socket in /var/run/screen/S-user.
Resume a screen session
To resume a detached session:
If the session has a name:
- screen -r session_name
If the session has no name or there are multiple sessions, list them first with:
- screen -ls
Then resume it using the session ID:
- screen -r 1234
Terminate a screen session
To end a screen session, you can do it in several ways:
From inside the session
- exit
or press:
- Ctrl + d
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 inside screen
While you are inside a screen session, you can use several commands.
Detach the session:
- Ctrl + a + d
View the list of screen commands:
- Ctrl + a + ?
Create a new window within screen:
- Ctrl + a + c
Switch between windows:
- Ctrl + a + n
Close the current window:
- Ctrl + a + k
Advanced screen usage
Split the screen
You can split the screen to view multiple windows at the same time.
Split horizontally:
- Ctrl + a + S
Split vertically:
- Ctrl + a + |
Move between regions:
- Ctrl + a + Tab
Close the current region:
- Ctrl + a + X
Additional tips
- Custom configuration: You can customize screen behavior by editing the following file:
-
~/.screenrc
Session logging: To log everything that happens in a screen session, you can start recording with:
-
Ctrl + a + H
This will create a file in the current directory:
-
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 ...