Jun 152012
 

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.

Install

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

screen installation

It will take a minute or two to install.

Running Screen

Then, to use it, type…

screen bash

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.

Detach

To detach this terminal session, press

CTRL + A     release, and then press   D

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…
screen -list

screen -list

Reconnect

…and you can reconnect to an instance with…
screen -r

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

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.

  11 Responses to “Using Screen with Raspberry Pi to avoid leaving SSH sessions open”

  1. [...] WARNING! Once you press <enter> for the sudo make command it will tie up your Pi for about 3.5 hours until the compile is finished. (If you are accessing your Pi via ssh, you might want to run it via screen, so you can detach and reattach without killing the process.) [...]

  2. Would you mind if I liked to this from my new Raspberry Pi blog? I don’t want to write a guide if there is a perfectly good one that exists already :-)

  3. [...] WARNING! Once you press <enter> for the sudo make command it will tie up your Pi for about 3.5 hours until the compile is finished. (If you are accessing your Pi via ssh, you might want to run it via screen, so you can detach and reattach without killing the process.) [...]

  4. or you start any process with “nohup ” to detach it from the console, stop it with STRG + Z and push it to the background with the “bg” command. You may safely log off now. To get your process back you can look with “jobs” for active jobs and get it back in foreground with with “fg”. any generated output can be found in “nohup.out”, so check with cat/less/more if your compile has finished

  5. [...] your circuit is wired up, power up the Pi with your battery and run the script. I ran it using screen so I could detach from it and log off (in order not to waste battery [...]

  6. [...] that the program ‘screen’ is installed Put the above script on your Pi (/home/pi is a good [...]

  7. You can use screen with name.

    create: screen -SO name_of_screen
    reconnect: screen -r name_of_screen

  8. Not sure if it’s in the repos for the Raspberry Pi distros, but Byobu is also worth a look – it’s a wrapper for screen that gives a more functional interface, some more keybindings, etc

Leave a Reply