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
| Action | Keys |
|---|---|
| Detach | Ctrl + A → D |
| Create new window | Ctrl + A → C |
| List windows | Ctrl + A → " |
| Next window | Ctrl + A → N |
| Previous window | Ctrl + A → P |
| Force kill session | Ctrl + A → K |
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 -rsays 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)— usescreen -d -rin that case - On shared VPS environments,
screen -lsonly shows your own sessions - Nesting screen inside screen causes key binding conflicts — avoid it
- For long-term use,
tmuxoffers more features and is worth considering
Recommended Cloud Hosting
Looking for reliable cloud infrastructure? Check out these developer-friendly services.
- Cherry Servers - High-performance VPS and dedicated servers
- Cloudways - Managed cloud hosting for developers