Let's talk about a useful command line app:
screen. It's 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 which might prevent you from learning it. This is a shame because 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.
The 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 python1
This 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 += 1
You 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.
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
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 python1
Technically 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 python2
Here we can also start a counting process. This is a good time to confirm that indeed we can see multiple processes running the background from the same shell. If you close and open the shell you can still retreive all the screens.
If 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
d is for detach, not destroy and
k is for kill here. This will also kill the screen that you are currently in.
Why is this so useful? Often you'll be running a long lasting job on a server that has a lot of resources for computation. You'll usually have an ssh connection and you don't want to loose results because the connection broke. A lot of people will even start their ipython notebooks in such a screen to guarantee that it runs like a deamon.