Screen is a unix/linux application that allows you to run multiple processes from an interactive shell, such as Apple’s Terminal application. This nifty utility also allows you keep processes running when you close out of an interactive shell session (if you have experience working with interactive shells, you know that when you close a shell the processes running in that environment will stop running).
Screen is extremely useful for working with the Raspberry Pi, or any other remote computer to which you connect using ssh. Here are two scenarios that illustrate why I love screen. If you are intrigued, read on to learn how to use screen.
- You are logged into the Raspberry Pi, using ssh, running an http server that publishes data from a sensor connected to the RPi. All of a sudden you get a “Connection Closed” message. Your session is abruptly closed and your http server gets shutdown because it was linked directly to your session. If you were running your http server using screen, then it would have continued running even when your session ended.
- Let’s say that you want to run two different node servers on a remote computer. You could open two separate ssh connections to the remote computer, and start-up a node server in each one. In order to keep the servers running you will need to keep both sessions open indefinitely. With screen, you can connect to the remote server and create two separate screens, each one running its own node server. Best of all, you can log out of your session and the screens will continue to run the node servers.
Now that I’ve given you my description of what screen does, and why it rocks, here is a the official description from the man page: “Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).” Check out the Screen User’s Manual for more details.
Quick Start Guide to Using Screen
I am not a screen (or Unix/Linux) expert. So here is my short cheat sheet with all the screen commands that you will need to get started using this powerful utility.
Find Out What Screens are Running: When you run the command below you will get a list of all screens that are currently running. The name of each screen will be preceded by a process number and followed by the screen’s status. When the status is “attached” that means that a session is currently logged into that screen; “detached” means that the no one is logged into the screen.
$ screen -ls
Create New Screen: In order to create a new screen just run the command below, replacing the screen_name with an alphanumeric identifier. You can create two screens with the same name, though I don’t recommend it. If you do so, you can use the unique process id to differentiate between them.
$ screen -S screen_name
Connect to Existing Screen: To connect (or attach) to an existing screen you need to know it’s name or process id number. Then just run the command below using the appropriate identifier.
$ screen -r screen_name
Detach from Screen: To detach from a screen just use the key stroke combinations described below. Note that to “detach” means to disconnect from a screen without closing it. After detaching from a screen I always confirm that it is still running by using the list screens command described previously.
'ctrl-a' followed by 'd'
Closing a Screen: To close a screen you need to use the key stroke combinations described below. Remember that when you close a screen it will shut down any processes that were running in the screen.
'ctrl-a' followed by ':quit' then 'enter' key