Using Screen with Raspberry Pi to avoid leaving SSH sessions open (Debian)
Tested on Debian Squeeze, Wheezy Beta and Raspbian distros.
You may remember from the Get_iplayer installation instructions that I recommended not running the FFMPEG compile via ssh. The reason for this was because when you do something in ssh, if you close the ssh session it kills the process – game over (and that compile takes 3.5 hours).
Well I just found out about an excellent little program called screen, which enables you to run processes within a “terminal tty instance”. This allows you to disconnect and reconnect the instance containing the process. You can even log off the ssh session altogether and connect from a different computer, then reconnect the terminal tty instance and the process will still be there doing whatever it was doing when you disconnected. Cool.
It’s really quick and easy to install. And easy to use too.
Here’s the text instructions, which are easy to follow. There’s a video at the bottom of the page.
sudo apt-get install screen
It will take a minute or two to install.
Then, to use it, type…
It will open another terminal instance. You can now start a process you want to be able to leave running and reconnect to later. For example, downloading something with get_iplayer.
To detach this terminal session, press
CTRL + A release, and then press
Then you are back in the original terminal screen with the other one running detached in the background.
List all Instances
You can list all open screen instances and their status by typing…
…and you can reconnect to an instance with…
If you only have one screen instance open, just -r will be enough. If you have more than one, you have to specify which one you want to reconnect with by typing its name after the -r
In this case…
screen -r 1245.pts-0.raspberrypi
Terminate an Instance
To end a terminal instance you need to be in that instance, then
CTRL + D and it will end instantly (no warnings).
And here it is on Video
The sound might be a bit quiet – you might need to turn it up a bit.
This is tremendously useful if you want to leave a process running and possibly even ssh in from a different machine later on. It will even survive a dropped connection as well. Enjoy.