screen is one of the most useful tools out there from the command line. Like a lot of command line tools the usecase can feel a bit niche when you are starting out while basic understanding of the tool will end up saving you a whole lot of time.
The main usecase I have for it is that I often ssh into a machine where I want to run a long lasting batch job ( spark-submit or some cross validation for sklearn ) and I don't want the job to stop when I am no longer connected. This document contains the minimum viable tutorial of commands that you need to do this.
Boot UpThe idea behind screen is that you have sessions that you can attach or detach to. The process inside of a screen will still continue running even if you're not looking at it.
Let's start a session with a name.
$ screen -S python1This is a new shell session in the same window. To prove that this we will run a python app in this view, detach from it and attach back again.
To start, run this python code from the current session;
import time i = 0 while True: time.sleep(2) print i i += 1You should see it count without stopping. From here we can exit the session via CRTL + a and then pressing d (for detach). You'll notice you've just popped back into your original shell.
Notice that CRTL + a is a shortcut that gives you access to shell and then d is the command that is passed to it. Even when you cannot type anything in the command line, you can still call CTRL + a and talk to screen .
Even though that you are outside of the python1 screen, you can view all of your screen processes via:
$ screen -ls There is a screen on: 49566.python1 (Detached) 1 Socket in /var/folders/mn/nf34qzvd11v3kd023kzyvzm00000gn/T/.screen.We can attach back to the detached screen via:
$ screen -d -R python1Technically I am first detaching the current screen ( -d ) and then reattaching with ( -R ) to a new screen. Once you're attached again you should notice that python is still counting. You can close down the terminal. Once you open up a new terminal you can see that the python process is still counting. The process in the screen with the name python1 will run even if you're not looking at it.
We can add a second screen by repeating previous steps.
$ screen -S python2And here we can also start a counting process.
Boot DownIf you are in a screen that you are done with you can either kill python and run the exit command or you can use CTRL + a and then press k .