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:

screen

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

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, then c.

  • Switch between windows: Press Ctrl + a, then n (next window) or p (previous window).

  • Detach from screen session: Press Ctrl + a, then d.

  • 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 simply screen -r to reattach to the last session.

Examples

Starting a screen session and running commands

  1. Start a new screen session:

    screen
  2. Inside the screen session, run commands as usual:

    ls -l
    cd /path/to/directory
  3. Create a new window within screen:

    • Press Ctrl + a, then c.

    • You are now in a new shell within screen.

  4. Switch between windows:

    • Press Ctrl + a, then n to move to the next window.

    • Press Ctrl + a, then p to move to the previous window.

  5. Detach from the screen session:

    • Press Ctrl + a, then d.

    • This leaves the screen session running in the background.

  6. 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