Using GNU Screen for Long-Running Processes
Using GNU Screen for Long-Running Processes
Ever started a long-running process over SSH, only to have your connection drop and kill everything? GNU screen solves this problem by creating persistent terminal sessions that keep running even after you disconnect.
What is Screen?
Screen is a terminal multiplexer that allows you to: - Run processes that survive SSH disconnects - Detach and reattach to sessions anytime - Run multiple terminal windows in one SSH session - Share terminal sessions between users
Think of it as "tabs for your terminal" that persist even when you close your SSH connection.
Installation
Fedora/RHEL/CentOS
sudo dnf install screen
Debian/Ubuntu
sudo apt install screen
Verify Installation
screen --version
# Output: Screen version 4.09.01 (GNU) 20-Aug-23
Basic Usage
Starting a New Session
Start with a name (recommended):
screen -S build
Start without a name:
screen
You're now inside a screen session. It looks exactly like a regular terminal, but it's persistent.
Detaching from a Session
To leave a session running and return to your normal terminal:
Press: Ctrl+A then D
You'll see:
[detached from 12345.build]
Your process keeps running in the background!
Listing Active Sessions
screen -ls
Output example:
There are screens on:
12345.build (Detached)
67890.download (Detached)
2 Sockets in /run/screen/S-martijn.
Reattaching to a Session
If you have one session:
screen -r
If you have multiple sessions:
screen -r build
Or use the PID:
screen -r 12345
Killing a Session
From inside the session:
exit
From outside:
screen -X -S build quit
Real-World Example: Building a RAG System
Here's how I use screen for long-running AI model builds:
# Start a named session
screen -S rag-build
# Navigate to project
cd ~/lennyhub-rag
source venv/bin/activate
# Start long-running process
python build_lightrag_direct.py 2>&1 | tee build.log
# Detach safely: Ctrl+A, then D
Later, even after closing SSH:
# Reconnect to SSH
ssh user@server
# Reattach to see progress
screen -r rag-build
# Process is still running!
Essential Screen Commands
All screen commands start with Ctrl+A, then another key:
| Command | Action |
|---|---|
Ctrl+A then D |
Detach (leave session running) |
Ctrl+A then C |
Create new window |
Ctrl+A then N |
Next window |
Ctrl+A then P |
Previous window |
Ctrl+A then K |
Kill current window |
Ctrl+A then [ |
Enter scrollback mode (scroll with arrows) |
Ctrl+A then ? |
Show help |
Ctrl+A then " |
List all windows |
Advanced Tips
Name Your Sessions Descriptively
screen -S model-training
screen -S data-processing
screen -S web-scraper
Better than generic names or PIDs!
Monitor a Session Without Attaching
screen -x build
This creates a "shared" view - useful for watching progress without interrupting.
Start Screen with a Command
screen -S download wget https://example.com/large-file.zip
Starts screen, runs the command, and you can detach immediately.
Log Session Output
# Inside screen, press Ctrl+A then H
# Creates screenlog.0 in current directory
Or start with logging enabled:
screen -L -S build
Reattach Even if "Attached" Elsewhere
screen -dr build
The -d flag detaches it from the other location first, then reattaches to you.
Common Use Cases
1. Long-Running Builds
screen -S compile
make -j$(nproc)
# Ctrl+A, D to detach
2. Server Monitoring
screen -S monitor
htop
# Ctrl+A, D to detach
3. Multiple Services
screen -S services
# Create windows for each service
Ctrl+A, C # New window
./start-database.sh
Ctrl+A, C # Another window
./start-api.sh
Ctrl+A, C # Another window
tail -f logs/app.log
# Switch between with Ctrl+A, N (next)
4. Remote Pair Programming
# Person A creates shared session
screen -S pairing
# Person B joins the same session
screen -x pairing
# Both see the same terminal in real-time!
Troubleshooting
"Cannot open your terminal '/dev/pts/X'"
script /dev/null
screen
Orphaned Screen Sessions After Reboot
# Clean up dead sessions
screen -wipe
Accidentally Nested Screen Sessions
Never run screen inside a screen session! Check if you're already in one:
echo $STY
# Empty = not in screen
# Has value = already in screen
Screen vs tmux
Screen: - Simpler, easier to learn - Available everywhere - Perfect for basic use cases
tmux: - More modern, more features - Better window management - Scriptable configuration
For most users, screen is perfect. I've used it for 15+ years without needing tmux.
Quick Reference Card
# Session Management
screen -S name # Start named session
screen -ls # List sessions
screen -r name # Reattach to session
screen -dr name # Detach & reattach
Ctrl+A, D # Detach from session
exit # End session
# Window Management (inside screen)
Ctrl+A, C # Create window
Ctrl+A, N # Next window
Ctrl+A, P # Previous window
Ctrl+A, " # List windows
Ctrl+A, K # Kill window
# Other
Ctrl+A, [ # Scrollback mode (q to exit)
Ctrl+A, ? # Help
Conclusion
Screen is an essential tool for anyone working with remote servers or long-running processes. Once you start using it, you'll wonder how you ever managed without it.
Key takeaways:
- Always use named sessions (-S name)
- Detach with Ctrl+A, D
- Reattach with screen -r name
- Use screen -ls to see what's running
Now you'll never lose work to a dropped SSH connection again!
Resources
- Official documentation:
man screen - Quick start:
screen -h - Help inside screen:
Ctrl+A, ?
Pro tip: Set up aliases in your .bashrc:
alias sls='screen -ls'
alias sr='screen -r'
alias ss='screen -S'
Then starting a session is just: ss build 🚀