A useful Linux command to run different commands on remote server when logged in via SSH is screen. This command is also very useful when your SSH connection drops and you want to resume from where you left. The screen command as the name suggests creates and manages different screens or terminals (similar to different workspaces but for the command line). You can then run different commands in each of these screens, disconnect them without ending the commands and reconnect them as needed.
It is also helpful when you don’t want multiple SSH sessions for running different Linux commands on the remote server. Also, it is great for executing a command that can take a while to complete like file transfers and so on.
For Ubuntu, Linux Mint and Debian, it can be installed using :
It’s use is the same regardless of the Linux distro being used.
Here is how to use it:
Making new screens:
First, open Linux command line and type:
This will bring up the information screen about it.
Now, hit Space to exit and type :
It will now show that a screen is active and attached. To create another screen session, type in:
It is a good idea to name different screens based on the tasks or purpose for them. So for example, to monitor say file transfers using rsync over ssh, name a screen accordingly. To monitor processes or check network connectivity, assign meaningful names and so on.
This will now display a blank screen indicating that the new screen is now active. You can now type in various Linux commands just like from the command line.
Switching between screens:
To create another screen directly from an existing one, you can also use Ctrl-a and c keys. This however will create screen sessions without any names so using the -S option is better.
Also, as these screens are created, you can cycle back and forth between them. To do that, first press Ctrl-a and then the p key (previous screen) or the n (next screen) key. Commands if running in specific screens will be displayed (htop and ping in two different screens in this example ) when cycling between the screens.
Once these different screens are active, you can also directly drop back to the command line by pressing Ctrl-a and d. Whatever commands are running in each of these screens will continue to do so.
Attaching and detaching screens:
Now when the screens are listed again, they will have different statuses like Attached or Detached.
This is useful when you want to run some commands but you keep disconnecting from the remote server either due to connectivity, idle session timeout or for not wanting to wait for commands to finish and remain logged in for the entire time. Simply make a screen for them and run the commands in there, then detach them. Even if you get disconnected, log back in and resume/reattach the active screen.
To resume detached screens, type in:
When you then list the screens again, the specific screen status would have changed from Detached to Attached.
You can detach and resume any screen as needed with the d and r parameters.
To close and exit any of the screens, simply type in:
There will be a message of screen terminating, meaning that the current screen session is being closed. Again, if you don’t want to close the active screen but directly switch back to the command line, use Ctrl-a followed by d key.
You can also lock your screen session using the x parameter. To do that for the specific screen, go there and use the Ctrl-a and x key.
The screen session will then ask for your password to unlock it.
A List of common screen commands:
You can also get a list of key combinations by using Ctrl-a and ? when on an active screen.
Here is a quick list of some common screen commandsÂ and what they do:
Also, to get details on various functions, use the man command :
This is a very handy tool when you want to manage your remote connections via SSH without having to retype commands after you get disconnected and relogin. However if the SSH server itself restarts or experiences network outage, then these screens will be lost.