screen is a program which allows you to run multiple terminal instances (think of them each as a window), switch between them and even detach and reattach to one later on. I will provide you with a few examples of how this works and why it is so useful:
- I often compile software remotely, this can sometimes take many hours to complete. I would ssh into a server, create a “screen” and then run the compilation jobs inside of it. Now I can detach from this “screen” and do other tasks on the same server while the compilation is running in the background. I can even disconnect entirely from my ssh session and re-attach to that screen later.
- When my internet connection is intermittent or for whatever reasons, the ssh session is unstable I can setup a “screen” on the remote server and it will remain persistent even if I disconnect. So when I reconnect and reattach to my “screen” I can resume my work where I left off. If I was compiling software like in the above example, it wouldn’t break the session and it would continue going, waiting for me to reattach later and see the results rather than just failing completely and wasting hours of work.
This feels very hard to explain but trust me, its easy and will be a lifesaver in the above circumstances as well as plenty more.
How to use screen:
- Install screen – If you are on macOS you already have it and many unix-like operating systems may have it pre-installed as well. Otherwise you will need to consult your package manager’s documentation on that (apt-get install screen / yum install screen / pacman -S screen / etc…)
- Start screen – you simply run it by executing: screen
- You will be given a terminal screen and you can run whatever work you like from here
- Detach from screen – to get out of this new terminal screen, press CTRL+a and then CTRL+d
- To reattach to the running screens, enter: screen -r
- You can also create multiple screens. Make sure you are attached to a screen already and then press: CTRL+a and then press c
- You will now have a new screen and the same fresh prompt waiting for you to run something
- You can list all of your screens by pressing: CTRL+a and then w
- To switch between between the screens you press: CTRL+a and then the screen #, for instance: CTRL+a and then 0 and then to switch back to screen 1 you press CTRL+a and then 1 (Please remember that it starts from 0, goes to 9)
- You can flip back and forth quickly between your current and previous screen by just tapping: CTRL+a and then hitting a again (think ALT+TAB’ing!)
- To outright kill a screen the command is CTRL+a and then k
- To log your screen’s activity, you press CTRL+a and then SHIFT+H
screen is a great terminal-window manager and I use it nearly every day. It allows me to just run one local terminal window and I can create a number of virtual spaces to run all of my different jobs on. It has saved my hundreds of hours from having to restart jobs, losing unsaved work and also eliminating the need to use multiple terminal clients locally to connect to the same server.
You can also use this locally of course, I tend to use it remotely since it adds a layer of protection against losing the connection and everything with it but local use can at least save you the hassle of managing a number of GUI terminal windows.