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.

  25 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

  9. Note that if you have many screens or just don’t like typing you can tab complete the -r command.
    “screen -r 1” will complete to “screen -r 1245.pts-0.raspberrypi”

  10. i’m having a process running on the console (no gui) on my remote rpi and i want to control it form far (with http://www.weaved.com). if i open an ssh window i don’t see the headless pi’s console. and screen doesn’t give me an entry in screen -list. how could i get this working? can i make the program open in an accessible screen?

  11. All I got was ‘Unable to locate package screen’. Is there something I need installed already? Or am I just doing it wrong?

    • What version of Raspbian are you using?
      Try running ‘sudo apt-get update’ before running ‘sudo apt-get install screen’ .

  12. Weird, ‘screen’ got lost in the last few weeks:

    tai@mystic:~ $ sudo apt-get install screen
    [sudo] password for tai:
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Package screen is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Package ‘screen’ has no installation candidate

    tai@mystic:~ $ date
    Thu 12 Apr 12:13:54 UTC 2018

    tai@mystic:~ $ uname -a
    Linux mystic 4.14.30+ #1102 Mon Mar 26 16:20:05 BST 2018 armv6l GNU/Linux

  13. Once screen is running, is there a way of seeing what program is running under each screen PID shown under the -list option? Specifically, I want to make sure a particular program is always running. I can always “ssh XX@XX screen -list” into the remote pi, but I see (for example) “1358.pts-0.name” instead of “MusicPlayer”. I can normally assume that I only have one screen shell running at any given time, but I hate assuming.

  14. tmux seems to be way better than screen, you can have the various sessions like panes on the screen. Works for Raspberry Pi.
    sudo apt-get install tmux

Leave a Reply