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 [OPTIONS] [SESSION NAME]
1. Create a session with specified name
screen -S [file]
-S option creates a new window within the screen and names it.
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
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
This option displays all currently open windows, including those running in the background.
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:
67890.pts-1.hostnameare the identifiers for the screen sessions.
67890are the process IDs (PIDs) of the screen sessions.
pts-1indicate the terminal types.
hostnameis the name of the host machine where the screen sessions are running.
Detached) or (
Attached) indicates the status of the screen session.
Detachedmeans that the session is running in the background and not currently attached to any terminal.
Attachedmeans that the session is currently being viewed in a terminal window.
/var/run/screen/S-usernameindicates 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.
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
Run your long-running process:
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:
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]
-d option is used to detach the screen session, allowing it to be reattached later.
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 “
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
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.
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 “
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:
|File System Operations||
|Search and Text Processing||
|System Information and Management||
|User and Session Management||