This document covers the screen session management system implemented in the Common role for persistent terminal sessions.
The deployment system automatically configures persistent screen sessions that survive SSH disconnections, providing a robust development environment for remote work.
The system installs and configures GNU Screen:
# From common role
- name: Install screen for persistent sessions
apt:
name: screen
state: presentDefault Session Name: DEV (configurable via SESSION_NAME variable)
Log Directory: ~/.claude-code-vm/logs/screen/ (in hidden deployment directory)
Configuration Variables:
screen_session_name- Session name (default: fromcustom_session_nameorDEV)screen_logs_dir- Log directory path (default:{{ deployment_base_dir }}/logs/screen)
The system creates a connect-session.sh script that:
- Checks Screen Availability - Verifies screen is installed
- Creates Screen Configuration - Sets up
~/.screenrcif not present - Sets Unicode Support - Configures UTF-8 locale for proper character display
- Session Logic:
- Reattaches to existing session if available
- Creates new session if none exists
- Handles session detachment/reattachment automatically
The system creates a ~/.screenrc file with:
# Screen configuration for persistent sessions
startup_message off # No startup message
defscrollback 10000 # 10,000 line scrollback buffer
hardstatus on # Enable status line
hardstatus alwayslastline # Status at bottom
hardstatus string "%-w%n %t%+w %= %H %m/%d %C%a" # Status format
termcapinfo xterm* ti@:te@ # Terminal compatibility
defutf8 on # UTF-8 support
defflow off # No flow control
vbell off # No visual bell
autodetach on # Auto-detach on hangupThe system adds automatic screen attachment to ~/.bashrc:
# Auto-attach to screen session for SSH connections
if [[ -n "$SSH_CONNECTION" ]] && [[ -z "$STY" ]] && [[ $- =~ i ]]; then
if command -v screen >/dev/null 2>&1; then
exec ~/.local/bin/connect-session.sh
fi
fiThis ensures that SSH connections automatically attach to the persistent session.
When connecting via SSH, the system automatically:
- Checks if screen is available
- Creates or reattaches to the named session
- Provides feedback about session status
Use the deployed script manually:
~/.local/bin/connect-session.shList sessions:
screen -listDetach from session:
# Press Ctrl+A, then DReattach to session:
screen -r DEVKill session:
screen -S DEV -X quitThe screen session system creates:
~/.claude-code-vm/
├── logs/
│ └── screen/ # Screen session logs
└── scripts/
└── connect-session.sh # Session management script
SESSION_NAME- Screen session name (default:DEV)DEPLOYMENT_DIR- Base deployment directory (default:~/.claude-code-vm)
screen_session_name- Session name fromcustom_session_nameor defaultscreen_logs_dir- Log directory pathdeployment_base_dir- Base directory for deployment filesscripts_dir- Directory for utility scripts
- SSH connects to the VM
- Bashrc executes and detects SSH connection
- Screen check verifies screen is available
- Session logic:
- If session exists: detach others and reattach
- If no session: create new persistent session
- Unicode support is automatically configured
- Sessions survive SSH disconnections
- Sessions persist through network interruptions
- Multiple SSH connections can attach to the same session
- Session state is maintained between connections
The system configures proper UTF-8 support:
export LC_ALL=C.UTF-8
export LANG=C.UTF-8Set custom session name via Makefile:
make deploy VM_HOST=192.168.1.100 TARGET_USER=developer SESSION_NAME=MyProjectOverride the base directory:
make deploy VM_HOST=192.168.1.100 TARGET_USER=developer DEPLOYMENT_DIR=/custom/pathModify ~/.bashrc to comment out the auto-attachment block if needed.
If screen is not installed, the connect script falls back to regular shell:
Error: screen is not installed. Please install it with: sudo apt install screen
Falling back to regular shell...List all sessions:
screen -listForce detach and reattach:
screen -d DEV
screen -r DEVRemove dead sessions:
screen -wipeThe system automatically sets UTF-8 locale. If characters display incorrectly:
export LC_ALL=C.UTF-8
export LANG=C.UTF-8The screen session system integrates with:
- Docker containers - Run containers within persistent sessions
- Kubernetes tools - kubectl commands persist across disconnections
- Node.js development - npm processes continue after SSH disconnection
- Git operations - Long-running git operations survive network issues
- Claude Code CLI - AI interactions persist in the session
This screen session management provides a robust foundation for remote development work with automatic session handling and UTF-8 support.