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
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
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
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.
[…] 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.) […]
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 :-)
No worries. What’s your blog’s emphasis going to be?
Mostly just tips and guides for beginner users, and things that I learn or helpful things to know specific to the RPI :-)
[…] 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.) […]
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
[…] https://raspi.tv/2012/using-screen-with-raspberry-pi-to-avoid-leaving-ssh-sessions-open […]
[…] 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 […]
[…] that the program ‘screen’ is installed Put the above script on your Pi (/home/pi is a good […]
You can use screen with name.
create: screen -SO name_of_screen
reconnect: screen -r name_of_screen
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
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”
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?
You’d need to start screen first, and then run your program. And then at a later time you can re-connect to the screen session, and you should still find your program running there :)
thanks. i wasn’t getting it to work. the program opened not in a screen. i’m now trying to do it with byobu. but that looks like it consumes a lot of resources…
Maybe something here will help?
https://www.google.co.uk/search?q=launch+screen+with+command
thanks again. i’m mostly struggling with the program that is running on the headless rpi. it somehow fills the terminal constantly (http://www.daveakerman.com/?p=1719) which is not compatible with screen or byobu. do you have an alternative idea? it looks like an incredible simple thing. i just want to access the gateway from far and no having to interrupt or restart it…
*shrug*
Maybe you could ask the developer(s) of the gateway software you’re using, if there’s an option to have less output displayed, so that it works better with screen?
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’ .
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
My bad above ^^ — I just needed to run ‘apt-get update’ again after a previous round up upgrades and before the next round. :)
We’ve all done it :)
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.
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