Command - Running Long-Lived Commands with GNU Screen
When you’re connected to a remote server over SSH, it’s common to start a long-running script, deployment, backup, or build job. The problem is simple: if your connection drops, your command may stop too. GNU Screen solves this by giving you a persistent terminal session that stays alive even after you disconnect.
Screen is a terminal multiplexer, which means it lets you run multiple terminal sessions inside one window. You can detach from a session, reconnect later, and even split your workspace into multiple panes for monitoring different tasks at once.
Core Explanation
A basic Screen command looks like this:
sudo screen -S name ./command.shHere’s what each part does:
-
screen
Starts GNU Screen. -
-S name
Creates a named session calledname. Naming sessions makes them easier to find later. -
./command.sh
Runs your script or command inside the Screen session.
Useful Screen controls
-
Ctrl + A
Opens the Screen command menu. Think of this as the main shortcut prefix for Screen actions. -
Ctrl + D
Detaches from the current session. The command keeps running in the background. -
screen -r name
Reconnects to the named session. -
Ctrl + X
In some terminal setups, this may be used for custom behavior, but it is not the standard reconnect shortcut for GNU Screen. The most reliable way to reconnect is withscreen -r. -
Ctrl + Shift + S
Splits the screen horizontally in some terminal emulators. In Screen itself, splitting is usually done with Screen’s own key bindings after entering theCtrl + Amenu. -
Ctrl + Shift + Tab
Switches between tabs or windows in some terminal applications, but this is not a universal Screen shortcut. -
Ctrl + Shift + |
Splits vertically in some terminal emulators. Again, actual Screen split bindings can vary depending on your environment.
Practical Examples
Start a script in a named session
sudo screen -S backup ./backup.shThis is useful for backups, installs, migrations, or any script that should keep running if your SSH session closes.
Detach safely
While the job is running, press:
Ctrl + AthenCtrl + DThis returns you to your normal shell while leaving the Screen session active.
Reconnect to the session
screen -r backupIf you used a different session name, replace backup with that name.
List active sessions
screen -lsIf you forget the session name, this command shows all running Screen sessions.
Requirements
- GNU Screen installed on the system
- A Linux, Unix, or similar environment
- SSH access if you are using it on a remote server
sudoonly if your command requires elevated privileges
To install Screen on Debian or Ubuntu systems:
sudo apt install screenOn RHEL-based systems:
sudo yum install screenSummary
GNU Screen is a practical tool for keeping important terminal sessions alive. It is especially helpful for remote administration, automation, and long-running scripts. Once you learn how to start, detach, and reconnect to a session, you’ll have a much safer workflow for work that can’t afford to stop when your connection does.