Loading...

Lab 56: Persistent Sessions with screen

Use screen to keep long-running terminal work alive across SSH drops or disconnects. Start a named session, detach safely, reattach when you reconnect, enumerate sessions, and terminate the session when the work is complete.

core troubleshooting

Scenario

You are performing a long upgrade process on a remote server. You cannot risk losing progress if your SSH session drops. Your job is to use screen to run the work inside a persistent terminal session, detach and reconnect cleanly, then close the session when finished.

Operator context

Persistent session tooling is a baseline reliability skill for remote operations. It prevents interrupted upgrades, lost logs, and half-applied changes when connectivity is unstable.

Objective

  • Start a named screen session for long-running work.
  • Detach from the session without terminating it.
  • Reattach to the session later.
  • List available sessions to confirm state.
  • Terminate the session explicitly when finished.

Concepts

  • A screen session is a persistent terminal that continues running when your network connection drops.
  • Naming sessions reduces operator error and makes reattach workflows predictable during remote operations.
  • Detach is a control operation that keeps processes alive while returning you to the parent shell.
  • Session inventory (screen -ls) is an operational check for attached versus detached state.
  • Explicit termination prevents stale sessions and makes change completion unambiguous.

Walkthrough

Step 1 : Start a new named session.
Command
screen -S devsession

Starting a named session makes reattach and cleanup predictable. Use names tied to the task to reduce confusion during multi-session work.

# You are now inside the screen session.
# Start your long-running work here.
Step 2 : Detach without terminating the session.
Action
Ctrl+a then d

Detaching leaves the session running in the background. This is the control operation that preserves work across SSH drops.

Detached from 11234.devsession.
Step 3 : List sessions and confirm state.
Command
screen -ls

This is your inventory check. It confirms the session exists and shows whether it is attached or detached.

There is a screen on:
    11234.devsession    (Detached)
1 Socket in /run/screen/S-user.
Step 4 : Reattach to resume work.
Command
screen -r devsession

Reattaching returns you to the exact shell and process state inside the session. Use this after reconnecting or when continuing a long-running change.

Step 5 : Terminate the session when finished.
Command
screen -S devsession -X quit

Terminating explicitly avoids leaving stale sessions behind. This is part of clean operational hygiene after completing a change.

# Confirm removal:
screen -ls

Common breakpoints

screen: command not found

The package is not installed or the PATH is incomplete. Install screen using your distro package manager and confirm /usr/bin is present in PATH.

There are several suitable screens on

Multiple sessions exist, so screen -r is ambiguous. Use screen -ls and reattach by name or full ID.

Cannot reattach because a session is attached elsewhere

The session is already attached on another terminal. Locate the attached session via screen -ls and detach it from that terminal before reattaching here.

Orphan sessions accumulate

Stale sessions create confusion during troubleshooting and remote work. Maintain naming discipline and terminate sessions explicitly when the change is complete.

Cleanup checklist

Ensure no sessions are left running unintentionally and that your session inventory is clean.

Commands
screen -ls
# If the session still exists, terminate it:
screen -S devsession -X quit
screen -ls
Success signal

screen -ls shows no session for the completed work, and reattach attempts fail because the session no longer exists.

Reference

  • screen -S <name> : Starts a new named screen session.
    • -S : Assigns a human-readable session name.
  • Ctrl+a then d : Detaches from the current session without ending it.
  • screen -ls : Lists active screen sessions for the current user.
  • screen -r <name> : Reattaches to an existing session by name or ID.
    • -r : Reattaches to a detached session.
  • screen -S <name> -X quit : Sends a command to terminate a session.
    • -S : Targets a session by name or ID.
    • -X : Executes a screen command in the target session.
    • quit : Ends the session.