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.
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
screensession 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
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.
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.
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.
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.
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
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.
Multiple sessions exist, so screen -r is
ambiguous. Use screen -ls and reattach by name
or full ID.
The session is already attached on another terminal. Locate
the attached session via screen -ls and detach
it from that terminal before reattaching here.
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.
screen -ls
# If the session still exists, terminate it:
screen -S devsession -X quit
screen -ls
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+athend: 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.
-