Using the "screen" command to run multiple programs

What is screen?

Screen allows you to manage several interactive shell instances within the same “window.” It is similar to tabbed windows in a web browser. By using different keyboard short-cuts, you are able to shuffle through the windows, access any of them directly, create new ones, kill old ones, attach and detach existing ones.Detaching screens is extreamly useful, it allows you to logout and keep your program running.

Setting up screen

Before you use screen it is useful if you do some configuration before hand. screen is configure via the .screenrc file.

Copy the following into a file called .screenrc in your home directory:

# Remove the copyright message
startup_message off

# Message status string on bottom line
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'

# Default screens
screen -t shell1        0
screen -t shell2        1

To start screen type "screen" to a shell prompt:

abs4@research0:~$ screen

You will now have a display that looks something like this:

The bottom line of the screen is the status line. It shows the name of the computer (research0), the two shell screens, and the date and time. The active screen is surrounded by red parentheses. Screen commands are preceded by the CTRL+a key combination (<ctrl>a). Useful commands are:

  • <ctrl>a n - move to the next screen
  • <ctrl>a p - move the previous screen
  • <ctrl>a 0-9 - move to screen "n"
  • <ctrl>a c - create a new screen
  • <ctrl>a k - kills the current screen
  • <ctrl>a d - detaches the current screen sessions and returns to the shell prompt
  • <ctrl>a \ - kills all windows and quits screen

Using screen to run programs for a long time

screen is useful if you have a program you want to run for a long period of time and you either want to leave your computer or want more security from a failed internet connection.

type "<ctrl>a d" to detach the screen sessions - your programs will keep running. You can then logout, and later login and reattach to your sessions with the "screen - r" command:

abs4@research0:~$ screen

<screen sessions happen here - user type "<ctrl>a d" to detach sessions - programs keep running>

[detached from 52150.pts-4.research0]
abs4@research0:~$ logout

<a few hours later we login>

abs4@research0:~$ screen -r

<screen session show>

abs4@research0:~$ screen -r

Always kill all your screens (<ctrl>a \) and logout when you have finished working, as this stops the computer being cluttered with inactive processes.