GNU Screen is incredibly useful for maintaining persistent terminal sessions, but it wasn't designed to survive system reboots. When your Linux server restarts, all Screen sessions terminate because they're tied to the running processes of that session.
The fundamental issue is that Screen operates at the process level, not the filesystem level. Each Screen session is essentially a collection of processes running under your user account. When the system shuts down, these processes receive termination signals and can't preserve their state.
While you can't directly persist Screen sessions, here are practical alternatives:
1. tmux with Resurrect Plugin
tmux is a modern alternative to Screen that supports plugins:
# Install tmux and plugin manager
sudo apt install tmux
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# Add to ~/.tmux.conf:
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tpm'
# After setup, save sessions with prefix + Ctrl-s
# Restore with prefix + Ctrl-r
2. Automated Session Recreation
Create a script to automatically rebuild your common Screen sessions:
#!/bin/bash
# ~/rebuild_screen.sh
screen -dmS main
screen -S main -X screen -t shell1
screen -S main -X screen -t shell2
screen -S main -X stuff 'cd /var/www^M'
Then add this to your crontab with @reboot /home/user/rebuild_screen.sh
3. Terminal State Preservation
For individual commands, consider using nohup
or disown
:
# Example with nohup
nohup long_running_command > output.log 2>&1 &
- Use descriptive session names (
screen -S projectname
) - Document your common session layouts
- Consider using terminal multiplexers in combination with process supervisors like systemd
If you must use Screen, implement a comprehensive logging solution:
# In your ~/.screenrc
logfile /tmp/screenlog.%t.log
log on
This won't preserve the interactive session but will maintain command output.
GNU Screen wasn't designed to persist sessions across system reboots - it's fundamentally a process-oriented terminal multiplexer. When your Ubuntu server reboots, all running processes (including Screen sessions) receive termination signals. This is an OS-level behavior rather than a Screen limitation.
For true session persistence, consider these architectural approaches:
// Method 1: Using tmux with resurrect plugin
# Install tmux and plugins
sudo apt-get install tmux
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
// Add to ~/.tmux.conf
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @continuum-restore 'on'
While not perfect, you can implement a semi-automatic recovery system for Screen:
#!/bin/bash
# Save current screen sessions
screen -list | awk '/\\t/ {print $1}' | cut -d. -f2 > /var/run/screen_sessions
# In your startup scripts (/etc/rc.local):
while read session; do
screen -dmS $session
# Additional commands to restore state could go here
done < /var/run/screen_sessions
For production environments, consider these robust alternatives:
- systemd: Create service units for critical terminal sessions
- Supervisord: Process control system with automatic restart
- Docker containers: Ephemeral but with volume persistence
Here's a complete tmux setup that automatically saves/restores sessions:
# In ~/.bash_profile
if [[ -z "$TMUX" ]] && [[ -n "$SSH_CONNECTION" ]]; then
tmux attach-session -t ssh_tmux || tmux new-session -s ssh_tmux
fi
# Crontab entry for periodic saving
*/5 * * * * /home/user/.tmux/plugins/tmux-resurrect/scripts/save.sh