Loading...

Lab 56: Persistent Sessions with screen

Use screen to keep long-running terminal work alive across network 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, and 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.

What You’ll Practice

  • Creating a named screen session with -S.
  • Detaching from a session using the control sequence Ctrl+a then d.
  • Reattaching to a named session with screen -r.
  • Enumerating active sessions with screen -ls.
  • Terminating a session using screen -S <name> -X quit.

Walkthrough

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

Starting a named session makes it easy to target and reattach later. In real workflows, naming sessions after a task or change ticket reduces operator error.

Screen session 'devsession' started.
Step 2 : Detach from the screen session.
Action
Ctrl+a then d

Detaching leaves the session running in the background. This is the key behavior that preserves work across disconnects.

Detached from 'devsession'.
Step 3 : Reattach to the screen session.
Command
screen -r devsession

Reattaching returns you to the exact shell state inside the session. This is how you resume long-running work after a reconnect or after stepping away.

Reattached to screen session.
Step 4 : List available screen sessions.
Command
screen -ls

Listing sessions is your inventory check. It confirms what is running, whether a session is attached or detached, and which name or ID you should target.

There is a screen on:
    11234.devsession    (Detached)
1 Socket in /run/screen/S-user.
Step 5 : Terminate the screen session.
Command
screen -S devsession -X quit

Terminating a session explicitly avoids leaving “orphan” sessions behind. In real environments, stale sessions can confuse operators, waste resources, and hide unfinished work.

Session 'devsession' terminated.

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 screen session without ending it.
  • screen -r <name> : Reattaches to an existing session by name or ID.
    • If multiple sessions exist, use screen -ls to identify the correct target.
  • screen -ls : Lists active screen sessions for the current user.
  • screen -S <name> -X quit : Sends a command to a session to terminate it.
    • -X : Executes a screen command in the target session.
    • quit : Ends the session.