Jump to content


From Wikitech

screen is a terminal multiplexer.

Once logged in on a server, use screen bash and you will have another "bash" in a virtual window. From there you can add other windows with different programs and move from a window to another. Ultimately, you can send "screen" in the background & logout - "screen" will detach and run in the background.


 hashar@bihash:$ ssh aserver
 $ screen bash

A new "bash" is invoked, my "xterm" title show:

 [screen 0: bash] hashar@zwinger:~

This means I am in the "screen" window #0 and running "bash".

 $ vmstat 1
 procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
  r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
  2  0  65508  51500 106756 430740    2    1     3     4    6     6  7 14 65 14
  0  0  65508  51372 106764 430732    0    0    12    24    0     0  2 15 82  2
  0  0  65508  51404 106768 430728    0    0     8     0    0     0  1 10 88  1

and so on ...

Multiple windows inside one "screen"

I am monitoring the server. Now let us create another window to edit a file. This is done by using CTRL + A then C (for create).

A new "bash" is invoked, my "xterm" title show:

 [screen 1: bash] hashar@zwinger:~

I can do whatever I want there. To move back to the window with "vmstat" use CTRL + A then P (for previous), to switch again CTRL + A then N (for next).

Splitting windows

You can also split a "screen" to see more than one window at once. Use CTRL + A then S (yes, that is a capital "S", so SHIFT + s, for Split), then use CTRL + A then <Tab> to switch between them. Now you can create a new window, using CTRL + A then C as described above and keep switching between them using Tab.

Detaching and Re-attaching

Finally it is dinner time or you need to reboot your computer, or whatever. You can send "screen" in the background and logout, that will keep all your program running. This is done with CTRL + A then D (for detach).

 $ screen bash
 $ ps fx
 13178 ?        S      0:00 SCREEN bash
 13179 pts/1235 S      0:00  \_ bash
 13249 pts/1236 S      0:00  \_ /bin/bash

"Screen" is in the background. Now you can logout. Once logged back, you want to resume your backgrounded "screen":

 $ screen -r

You are back to the "vmstat" stuff which is still spamming values :)

Finally log out of all windows, when there is none left, "screen" exit:

 [screen is terminating]

Attaching a shared "screen"

If you want to share a "screen" with somebody else who is logged in as the same user, or you have forgotten to detach from somewhere else, and screen -r does not appear to work use:

 $ screen -x

(Attach to a not detached "screen" session.)


From inside "become", "screen" currently (cf. phab:T52248) outputs an error:

 local-dbreps@tools-login:~$ screen
 Cannot open your terminal '/dev/pts/50' - please check.

You can work around this by running script /dev/null (you can first read man script to figure out what "script" does). A second workaround is running "screen" first and then running "become" from inside "screen". However, per the cloud listserv, "screen" should not be used by tool accounts and instead jobs should be submitted via "jsub".