logo

Practical Computing Advice and Tutorials

Sun: 22 Sep 2019


Site Content

Programming
&
Development


Technical Knowhow


Command Line Interface


Security

Linux | Screen

Screen allows us to multiplex between several processes within the computer. The motivation for using Screen is because if you, say, lose a SSH connection to the remote system for any reason, the process that was running will also die. But, if you've invoked the Screen command beforehand, when you re-establish the connection, or if you can get physical access to the machine, the process should still be running, in the background and you'll be able to reconnect to the screened process.

It can also be handy to hop between processes. As an example, if I'm reading the manual page of a process that I'm trying to master, I can switch between the manual page I'm reading and a CLI that I'm using the command with; a working example:

From the CLI type screen man SSH You'll now be looking at the manual page for OpenSSH. Now hold down the Ctrl key and, without releasing it, press a then d. If all went well, you should now be looking at a message that looks something like this...

[detached from 12532.pts-0.D630]
rob@D630:~$

The details will be different for you, but the context should be the same: pid.tty.host That's telling us the Process ID is running a Pseudo-Terminal-Slave on Hostname and we've detached from the process. To see a list of screens use the command screen -ls which is shorthand for list. You should see something like this...

rob@D630:~$ screen -ls
There are screens on:
19055.pts-0.D630 (22/10/17 23:03:42) (Detached)
12532.pts-0.D630 (22/10/17 22:57:03) (Detached)
2 Sockets in /var/run/screen/S-rob.
rob@D630:~$

Again, the details will differ with you, but the context remains. As you can see, I have two screens running in the background.

To reattach a screen, we use the -r switch, like this: screen -r 19055.pts-0.D630

You should find that auto-complete will work, so after typing screen -r 19 if I then press the TAB key, the rest will automatically fill in. Don't forget that pressing the up arrow key will scroll back through a history of the commands that have been entered. E.g. if I now double tap the up arrow key, the screen-ls command will be right there.

The more you use CLI, the quicker you'll become and discovering simple short cuts, such as the up arrow key and TAB key, are a good way of speeding things up.