screen

Manage multiple terminals

Screen: Your Gateway to Terminal Multiplexing

The screen command is a powerful terminal multiplexer that allows you to create, manage, and switch between multiple terminal sessions within a single SSH connection or terminal window. This tool is invaluable for system administrators, developers, and anyone who needs to run long-running processes or maintain persistent sessions.

What is Screen?

Screen creates virtual terminals that persist even when you disconnect from your session. This means you can start a process, detach from it, log out, and later reconnect to find your process still running exactly where you left it.

Essential Screen Commands

Starting a New Screen Session

screen

To start a named session (recommended):

screen -S session_name

Creating Background Processes

Once inside a screen session, you can run any command normally:

python long_running_script.py
./backup_database.sh

Detaching from a Session

To leave a session running in the background:

  • Press Ctrl + A, then D (detach)

Listing Active Sessions

screen -ls

This shows all currently running screen sessions.

Example output:

Reattaching to a Session

To return to a detached session:

screen -r session_name

If there's only one session:

screen -r

Managing Multiple Windows

Within a screen session:

  • Ctrl + A, then C - Create new window

  • Ctrl + A, then N - Switch to next window

  • Ctrl + A, then P - Switch to previous window

  • Ctrl + A, then " - List all windows

Terminating Sessions

To end the current session:

exit

Or press Ctrl + A, then K to kill the current window.

To forcefully terminate a detached session:

screen -X -S session_name quit

Some times you will see a confirmation dialogue at the bottom, something like this:

You can just type y and enter

Practical Example

# Start a named session
screen -S database_backup

# Run your long process
./backup_large_database.sh

# Detach (Ctrl + A, then D)
# Log out of server

# Later, reconnect
ssh your_server
screen -r database_backup

# Detach and kill the session
#(Ctrl + A, then K)

Why Use Screen?

  • Persistence: Processes continue running after disconnection

  • Organization: Multiple sessions for different tasks

  • Reliability: Protection against network interruptions

  • Efficiency: No need to restart long-running processes

Screen is an essential tool for anyone working with remote servers or needing to manage multiple terminal sessions efficiently. Master these basic commands, and you'll find yourself more productive and less worried about losing work due to connection issues.

Last updated