screen
screen
The screen
command in Unix and Linux is used to create and manage multiple terminal sessions within a single terminal window or SSH session. It provides a way to multiplex a physical terminal between several processes, typically interactive command-line sessions.
Basic Usage
To start a new screen
session:
This command starts a new session and opens a shell prompt within it. From this new screen session, you can start running commands and managing multiple terminal windows.
Key Commands in screen
screen
Once inside a screen
session, you can use various key commands to manage multiple windows, detach sessions, and reattach to them later:
Create a new window: Press
Ctrl
+a
, thenc
.Switch between windows: Press
Ctrl
+a
, thenn
(next window) orp
(previous window).Detach from screen session: Press
Ctrl
+a
, thend
.List windows: Press
Ctrl
+a
, then"
(lists all windows for selection).Attach to a detached session: Use
screen -r
followed by the session ID, or simplyscreen -r
to reattach to the last session.
Examples
Starting a screen
session and running commands
Start a new
screen
session:Inside the screen session, run commands as usual:
Create a new window within
screen
:Press
Ctrl
+a
, thenc
.You are now in a new shell within
screen
.
Switch between windows:
Press
Ctrl
+a
, thenn
to move to the next window.Press
Ctrl
+a
, thenp
to move to the previous window.
Detach from the
screen
session:Press
Ctrl
+a
, thend
.This leaves the
screen
session running in the background.
Reattach to the detached
screen
session:Use
screen -r
to reattach to the last detached session.
Practical Use Cases
Remote Sessions
screen
is invaluable for managing long-running processes or sessions on remote servers. If the SSH connection drops, screen
ensures that your session continues running in the background, and you can reattach to it later.
Managing Multiple Tasks
With screen
, you can multitask within a single terminal window, switching between different tasks or shells without opening multiple terminal windows.
Script Automation
Automate tasks and scripts within screen
sessions, ensuring that they run continuously even when disconnected from the terminal.
Summary
The screen
command is a powerful tool for managing multiple terminal sessions within a single terminal window or SSH session in Unix and Linux. It allows you to detach and reattach sessions, manage multiple tasks simultaneously, and keep processes running in the background even if the terminal session is terminated. Understanding its key commands and usage can significantly improve your workflow efficiency, especially in remote server management and multitasking environments.
help
Last updated