What I Wanted to Do

I wanted to run a long process on my VPS and close the terminal without killing it. Since closing the SSH connection kills the process, I used screen to persist the session.

Installing screen

# Ubuntu/Debian
sudo apt install screen

# CentOS/RHEL
sudo yum install screen

Check if it’s already installed with screen --version.

Basic Usage

Start a New Session

screen

A new session starts immediately. Naming it makes things easier to manage later.

screen -S mysession

Detach a Session (Send It to the Background)

Press Ctrl + A then D.

[detached from 12345.mysession]

This message means detach was successful. Even after closing the SSH connection, any process running inside the session will continue.

Reattach to an Existing Session

# List sessions
screen -ls
There is a screen on:
        12345.mysession (Detached)
1 Socket in /run/screen/S-user.
# Reattach by session name
screen -r mysession

# Or by ID
screen -r 12345

End a Session

Run exit inside the session, or press Ctrl + D.

Common Key Bindings

ActionKeys
DetachCtrl + AD
Create new windowCtrl + AC
List windowsCtrl + A"
Next windowCtrl + AN
Previous windowCtrl + AP
Force kill sessionCtrl + AK

Managing Multiple Sessions

# List all sessions
screen -ls

# Force quit a specific session (when you can't attach to it)
screen -S mysession -X quit

If you get an error reattaching to a detached session, use -d -r:

# Detach any other client and reattach
screen -d -r mysession

Pitfalls

  • If screen -r says the session isn’t found, try using the ID instead of the name
  • After reconnecting via SSH you may find the session listed as (Attached) — use screen -d -r in that case
  • On shared VPS environments, screen -ls only shows your own sessions
  • Nesting screen inside screen causes key binding conflicts — avoid it
  • For long-term use, tmux offers more features and is worth considering

Looking for reliable cloud infrastructure? Check out these developer-friendly services.