Skip to content

CHPC Software: Use of Linux command "screen"

NOTE: Due to certain limitations (the big one being selective import of environment variables), we discourage screen usage and promote tmux instead.

This document is to introduce users to the 'screen' command and help give some ideas of where it might be useful to use. The 'screen' tool allows a user to start up multiple shell sessions on a computer. There are a few points of value from using screen to do this vs just using something like ssh or xterm. One of the most important values is that 'screen' sessions will continue operation even if you loose connection to the remote system your running 'screen' on.

CHPC strongly suggests that users consider using screen for running applications or commands from a remote session that may be active for a long period of time. We will outline a few core features of using 'screen' in this document. However a thorough resource on 'screen' is available by viewing the 'screen' manpage.

Simply typing screen will start a 'screen' session. The session will look like a plain terminal session and you should be looking at a normal prompt.

You can type screen -ls at any time, and you should see a listing of all the 'screen' sessions that are currently active. For example:

u0108240@kingspeak2:~$ screen -ls
There are screens on:
        23294.gaussian  (Attached)
        7769.pts-12.kingspeak2  (Attached)
2 Sockets in /var/run/screen/S-
u0108240.u0108240@kingspeak2:~$

Each 'screen' session is identified with the process id number, followed by an informational piece that can be customized. To make the sessions more easily identifiable, 'screen' can be started with a -S flag to give the session id an identifier that is more easily recognized.  Notice in the above example that there is a session named with a "gaussian" suffix. This screen session was started using  screen -S gaussian . All commands given to 'screen' are given with special commands. In order to execute any command, you must first press 'ctrl+a', followed by a key that indicates the command. For example, ctrl+a, followed by ? will give a help screen (note that the carrot symbol (^) means 'ctrl').

Flags commonly used with the screen command:
screen -ls (lists your screen session)
screen -S  sessionname (name your screen session)
screen -r  (resume)
Common commands to use within a screen session (again ^ means the cntl key)
^a ? (Help)
^a d (Detach screen from this terminal)
^a 0 (Switch to window number 0 (or 1, 2, 3...9))
For a full list of screen flags and cntrl (^) commands:
man screen (from the command line)

Once you begin your application within your 'screen' session you are able to detatch from the session without closing your application. To do this you simply type: ctrl+a d. You can also type screen -d to accomplish the same. This detaches the 'screen' and drops you back to the shell from which you started the 'screen' session. Processes running within these detached 'screen' sessions will continue to operate as normal. You can exit from the ssh session and leave the screens going and come back to them at any time and from any other ssh session. This allows you to avoid complications from suspended ssh sessions or other possible mishaps (like disconnects).

To come back to the screen session at a later time, log in to the node you set the screen session up on and type screen -r. If you have more than one screen running, you can use screen -r 7769.pts-12.kingspeak2 as in the example above, substituting your session name.

To end 'screen' sessions you just type exit as you normally would to end a terminal session. This will terminate any programs you have running in the screen session, so make sure you're done with the session before you close.

Last Updated: 7/5/23