Screen commands

There may be times when you need a program to run independently of the current shell session. For example, you may want to ensure a long running process does not get terminated when you shut down your machine or in the event of a connection dropout. For this you can use the screen command. The screen command allows a user to open separate terminal instances inside a single terminal shell. The user can then detach an instance from the current session and then re-attach it at a later date. Below are some basic instructions on how to use the screen command.

(Note – the instructions/screenshots below have been generated using a mac environment. Steps/views may differ slightly based on your operating system)

New Screen session

To begin a new screen session, first open a terminal window (either directly from within mac/linux or via putty on Windows) and then type screen at the command prompt.

$ screen

(You may see an initial introduction screen at this point – Use the space bar or ‘return’ to continue to your new screen session).

At this point you are now operating from within a new screen session and can continue to issue linux commands or run programs as normal. You can even fire up further screen sessions by issuing the screen command again from within the current screen.

Screen sessions will automatically be given an identifier, such as ‘92259.ttys002.MC02M7ARMFH01’ (see below). If you would like to give names to each new screen session, you can use the following format:

$ screen -S name

Detach Screen

At any time you may detach from the current screen session (even whilst a program may be currently running). Detaching a screen session keeps the screen alive in the background but will return the user to the original terminal shell. To detach from a screen session, press CTRL+A followed by d

List all screen sessions

You may have multiple screen sessions running at once (all running different processes for example). To list all screen sessions, type screen -ls at the command prompt.

$ screen -ls
There are screens on:
	92259.ttys002.MC02M7ARMFH01	(Detached)
	92319.ttys002.MC02M7ARMFH01	(Detached)
2 Sockets in /var/folders/fn/bwlg_k8n5h33rfwr54hk5_6h0000gp/T/.screen.

In the example above, you can see that there two detached screen sessions (92259.ttys002.MC02M7ARMFH01 and 92319.ttys002.MC02M7ARMFH01).

Reattach a screen session

You can reattach to a particular screen session at any time by typing screen -r followed by the identifier associated with the screen session in question. For example in the example above we can reattach to the first screen session using:

screen -r 92259

*note that you only need to enter the numeric part at the beginning of the screen ID.

At this point you should now be returned to the screen session (with any ongoing processes still running).

Terminate screen sessions

To terminate a screen session, you ccan reattach to that session and type exit like any normal terminal shell. Or to kill a screen session from the original terminal window you can use the command screen -X -S id quit, e.g:

$ screen -ls
There are screens on:
	93142.ttys001.MC02M7ARMFH01	(Detached)
	93499.ttys001.MC02M7ARMFH01	(Detached)
2 Sockets in /var/folders/fn/bwlg_k8n5h33rfwr54hk5_6h0000gp/T/.screen.

$ screen -X -S 93142 quit
$ screen -ls
There is a screen on:
	93499.ttys001.MC02M7ARMFH01	(Detached)
1 Socket in /var/folders/fn/bwlg_k8n5h33rfwr54hk5_6h0000gp/T/.screen.


The steps below show a real world example of how to use the screen command to log (ssh) into a remote machine and set a large data transfer going from that remote machine to another remote machine.

Step 1

Open a terminal window (from linux or mac) or open a putty connection (from Windows)

Step 2

Start a new screen session (and give it a suitable name):

$ screen -S data-transfer

(press return or space if an intro screen appears)

Step 3

Log into the remote machine from where you want to initiate a data transfer (using ssh for example). Once successfully logged in begin the data transfer using an scp/sftp command etc (or any other process you may want to undertake)

Step 4

Detach from the session (even if the transfer is still underway) by using Ctrl-a followed by d

(you should now be back at your original terminal window).

At this point you can safely exit the terminal and shut off your machine (if so required).

Step 5

To reattach to the screen session carrying out the data transfer at any time (to check the status of the transfer for example), use screen -r

 $ screen -r data-transfer

Step 6

When the data transfer is complete, you can close the screen session by issuing the exit command

$ exit


You can now close the original terminal window.