Using Screen



 

 


The screen utility allows launching multiple shells in a single terminal session and provides means to navigate between the running shells.

Remember when you had a remote session running a program and the communications link failed? When that happened the running program was terminated as well and you would have had to restart it from the beginning.

The screen program allows disconnecting the screen session from the terminal session and reconnecting later from the same or a different computer. Another interesting aspect of this feature of using screen to perform CLI tasks on remote hosts is that the screen session will continue to run even if the connectivity to the remote hosts is broken intentionally using the Ctrl-a d key combination or if the network connection fails. All of the CLI programs running in the screen terminal sessions will continue to run. This means that once communications is reestablished one can log back into the remote host, use the screen -r command at the login command line to reattach the screen session to the terminal.

So I can start up a bunch of terminal CLI sessions in screen, use Ctrl-a d to disconnect from screen and logout. Then I can go to another location, login to a host, SSH to the host running screen, login and use the screen -r command to reconnect to the screen session and all of the programs will still be running.

The screen command can be useful in some environments where physical access to a hardware console is not available to provide access to the Virtual Consoles but the flexibility of multiple shells is needed.

You will find it convenient to use the screen program and in some cases it will be necessary to do so in order to work quickly and efficiently. Unfortunately, the screen command has not been installed as part of most default installations, so you may have to do this.

The network interface should have been started on your computer but if it has not you will have to start it.

You can install the screen package using the following command:

yum -y install screen

Remember that this is Linux, not Windows, so it is NOT necessary to reboot after installing most software. One of the very few exceptions would be after installing an updated kernel. The “yum” command, which is used to install remove and update software packages, is covered in more detail in another document.

Now to start screen and learn how it works, follow these steps.

  1. At the CLI type screen which will clear the display and leave you at a command prompt.
  2. Type any command such as ls to have something displayed in the terminal session besides the command prompt.
  3. Type Ctrl-a c to open a new shell within the screen session.
  4. Enter a different command, such as df –h in this new shell.
  5. Type Ctrl-a a to switch between screen shells.
  6. Enter Ctrl-a c to open a third shell.
  7. Type Ctrl-a “ to list the open shells. Choose any one except the last one by using the up/dn arrow keys and hit the Enter key to switch to that shell.
  8. To close one session, type exit and press the Enter key.
  9. Type the command Ctrl-a “ to verify that the shell is gone. Notice that the shell with the number you have chosen to close is no longer there and that the other shells have not been renumbered.
  10. To reopen a fresh shell use Ctrl-a c.
  11. Type Ctrl-a “ to verify that the new shell has been created. Notice that it has been opened in the place of the shell that was previously closed.
  12. To disconnect from the screen session, press Ctrl-a d. Note that this leaves all of the shells and the programs running in them intact and still running.
  13. Enter the command screen -list to list all of the current screen sessions. This can be useful to ensure that you reconnect to the correct screen session if there are multiple ones.
  14. Type screen –r to reconnect to the active screen session. If multiple active screen sessions are open, then a list of them will be displayed and you can choose the one to which you wish to connect.