How to use the command 'screen' (with examples)
The screen
command is a powerful tool that allows users to create and manage multiple terminal sessions within a single SSH connection. It is particularly useful when working on remote servers, as it enables users to keep their sessions open even if the connection is interrupted.
Use case 1: Start a new screen session
Code:
screen
Motivation:
When working on a remote server, starting a new screen session allows you to have multiple terminals running simultaneously. This can be helpful for multitasking or running different commands in parallel.
Explanation:
By running the screen
command without any arguments, a new screen session is started. This session will open a new terminal window, providing a separate environment for running commands.
Example output:
A new screen session is started, and a terminal window appears. You are now able to run commands within this session.
Use case 2: Start a new named screen session
Code:
screen -S session_name
Motivation:
Naming your screen sessions can make it easier to identify and manage them, especially when working with multiple sessions simultaneously. It allows you to quickly switch between sessions or detach from them and reattach later.
Explanation:
The -S
option followed by a session name allows you to start a new screen session with a specific name. This name can be used to identify and reference the session when executing other screen
commands.
Example output:
A new screen session named session_name
is started. This session can now be accessed or managed using its unique name.
Use case 3: Start a new daemon and log the output to screenlog.x
Code:
screen -dmLS session_name command
Motivation:
Starting a new screen session as a daemon allows the session to continue running even after you disconnect from the remote server. This is useful when you have long-running processes that need to persist.
Explanation:
The -d
, -m
, and -L
options combined create a new detached screen session that runs as a background daemon. The session_name
and command
arguments are used to specify the session name and the command to be executed within the session.
Example output:
A new detached screen session named session_name
is created, and the specified command
is executed within the session. The session is running in the background, and its output is logged to a file named screenlog.x
.
Use case 4: Show open screen sessions
Code:
screen -ls
Motivation:
When working with multiple screen sessions, it can be helpful to see a list of all the currently open sessions. This allows you to identify active sessions, their names, and whether they are attached or detached.
Explanation:
By running the screen -ls
command, you can view a list of all the open screen sessions on the server. The output includes the session ID, session name, and its current status.
Example output:
There are screens on:
1963.session_name (Attached)
2128.another_session (Detached)
2 Sockets in /run/screens/S-user.
Use case 5: Reattach to an open screen
Code:
screen -r session_name
Motivation:
Reattaching to an open screen session allows you to regain access to a previously started session, even after you disconnect from the remote server. This is useful when you need to continue working on a specific session without losing your progress.
Explanation:
The -r
option followed by the session name is used to reattach to an open screen session. This command brings the session back to the foreground and allows you to continue working within it.
Example output:
The specified screen session named session_name
is reattached, and the terminal window displays the session’s environment. You can now interact with the session as if you had never disconnected.
Use case 6: Detach from inside a screen
Code:
Ctrl + A, D
Motivation:
Detaching from a screen session allows you to leave the session running in the background while you attend to other tasks or disconnect from the server. When you reattach later, you can pick up where you left off without interrupting the ongoing processes.
Explanation:
To detach from a screen session from within the session itself, press Ctrl + A
followed by D
. This key combination detaches your terminal from the session and brings you back to your shell prompt.
Example output:
After pressing Ctrl + A
followed by D
, the screen session is detached, and you are returned to your shell prompt. The session continues to run in the background, allowing you to perform other tasks.
Use case 7: Kill the current screen session
Code:
Ctrl + A, K
Motivation:
Ending a screen session is useful when you no longer need it or want to free up system resources. Killing the session terminates all processes running within it and removes it from the list of active screen sessions.
Explanation:
To kill the current screen session from within the session itself, press Ctrl + A
followed by K
. This key combination prompts for confirmation before terminating the session.
Example output:
After pressing Ctrl + A
followed by K
, a confirmation message appears asking if you want to kill the current screen session. If confirmed, the session and all its processes are terminated.
Use case 8: Kill a detached screen
Code:
screen -X -S session_name quit
Motivation:
Sometimes, you may have detached screen sessions running in the background that you no longer need. Killing these sessions allows you to free up system resources and remove unnecessary sessions from the server.
Explanation:
The -X
option followed by -S session_name
is used to send commands to a specific detached screen session. By executing screen -X -S session_name quit
, you send the quit
command to the detached session, causing it to terminate.
Example output:
The detached screen session named session_name
is terminated, and all processes running within it are stopped. The session no longer appears in the list of open screen sessions.
Conclusion:
The screen
command provides a versatile and powerful way to manage multiple terminal sessions within a single SSH connection. By utilizing its various options and key combinations, you can create, manage, and detach from screen sessions, ensuring your work continues uninterrupted even in remote server environments.