screen: A Tool for Your Terminals You Should Know About

Thread Starter

Ya’akov

Joined Jan 27, 2019
5,690
The screen program, available in UNIX and Linux is something I use every day. I would be much less productive without it. But, while the systems administrators I spent time with during my learning and honing my sysadmin skills universally knew about, and used it, I have recently found that many people of later "generations" aren't familiar with it. If you use a terminal to manage your systems, or communicate, or edit you should be.

screen allows you to start interactive and non-interactive programs in a terminal, and then detach allowing them to continue to run in the background, something like the nohup program but preserving the terminal to which you can attach later and find what you left as well as any other output.

While it used to be part of a base install, it seems to have been removed and so you may have to install it to use it. In Debian-like distributions, you can use:

apt-get update && apt-get install screen

While screen can do a lot and can take time to learn completely, basic use which gets you a lot of utility is pretty easy. To start screen, you can just type screen without and argument. This will start screen with an informational banner and you can use it just like that, all the other commands apply.

More useful is to add some arguments. My favorite invocation looks like:

screen -S yaakov

The -S is for session name which can be used as a convenient way to reattach to a particular session. Once it is invoked this way, we can see the session with the -ls argument:

yaakov@rennie:~$ screen -ls
There is a screen on:
30791.yaakov (Attached)
1 Socket in /var/folders/qy/pyq5j2x90dxbs3s4k80wp1780000gn/T/.screen.


NOTE: You can specify a program to run when you invoke screen and when that program exits, so will screen.

You can see the session name after the PID. If you don't name the session, only the PID is available to specify which can be confusing. But now the fun part: detaching and reattaching. To send commands to screen while using it, use the escape character. By default, this is [CTRL] + a (^A). You can see all the key bindings by typing the sequence ^A ? which will display a sort of help page.

But the first command I want you to know is ^A ^D, which is detach. If I type it in the session I started above, the terminal window vanishes and I see:

yaakov@rennie:~$ screen -S yaakov
[detached]


And now screen, and the terminal inside it, is running in the background waiting for you. If you were using SSH to connect to the machine, you could disconnect without disturbing it. It will run indefinitely. Then to reattch to the session we can use:

screen -x yaakov

And that easily, we are back in the session we left earlier.

The -x tells screen to reattach in multi display mode which means that even if the screen session is currently attached somewhere it will attach to it. This is very useful for sessions that you might access from different places. There are several ways to (re)attach to sessions, and the screen(1) man page, linked in the introductory paragraph has a lot of information.

In the end, if all you use is what I have described, screen is still a great tool. But it can do much more, including adding windows to a session with ^A ^C (^A " is the easiest but not the only way to navigate these). If you haven't heard of screen, I hope you find this useful. It's a tool I would hate to lose and I think you'll also find it very useful.

[EDIT: There is another program called tmux which is newer and offers similar functionality to screen with some additional features particularly more robust windowing but I have not found it to be better for me than screen. You should investigate it if you are interested in getting more sophisticated with such programs since it is more widely supported by integration into terminal programs and the like than screen.]
 
Last edited:

Thread Starter

Ya’akov

Joined Jan 27, 2019
5,690
An additional bonus: screen is a VT100 terminal emulator and can be connected to a serial device like:

yaakov@rennie:~$ screen /dev/tty.usbmodem91402001
$GPTXT,01,01,02,u-blox ag - www.u-blox.com*50
$GPTXT,01,01,02,HW UBX-G70xx 00070000 FF7FFFFFo*69
$GPTXT,01,01,02,ROM CORE 1.00 (59842) Jun 27 2012 17:43:52*59
$GPTXT,01,01,02,PROTVER 14.00*1E
$GPTXT,01,01,02,ANTSUPERV=AC SD PDoS SR*20
$GPTXT,01,01,02,ANTSTATUS=OK*3B
$GPTXT,01,01,02,LLC FFFFFFFF-FFFFFFFF-FFFFFFFF-FFFFFFFF-FFFFFFFD*2C
$GPRMC,,V,,,,,,,,,,N*53
$GPVTG,,,,,,,,,N*30
...


Which connects screen to the USB connected serial port of a GPS module on my Mac. It's a handy tool for troubleshooting and configuration, and of course can be used with any supported USB → TTL serial adapter. To set the baud rate and other parameters you can use the stty program, or simply put the baud rate after the device name on the screen command line.
 
Top