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.
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.
screen session for long-running
work.
screen -ls) is an operational
check for attached versus detached state.
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
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.
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.
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.