Using screen - the basics

Rob

Administrator
Staff member
Credits
129
When you're connected via ssh on a remote server, a sudden disconnection will likely wreck whatever you're working on and make it difficult to find where you left off. This is where screen comes in handy. It's like working in a virtual terminal that will stay open until you exit out of it. If you suddenly lose Internet connectivity at your workstation (power loss at home, office, etc..) you can reconnect to your server, enter your screen session and continue where you left off.

screen isn't the only option, there's other popular ones like tmux and byobo, but being an old schooler, i'm still a fan of screen.

To set it up, ssh into your server and make sure it's installed:

Red Hat based:
Code:
yum install screen
Debian based:
Code:
apt-get install screen
Now that it's installed, let's create a simple screen session, disconnect, and reconnect.

Start screen by typing screen. You can add the -S flag to name it which is probably a good habit to get into:
Code:
screen -S test1
Ok, not very fancy, but you're now within a screen session. Let's start a process that won't stop on its own like:
Code:
ping google.com
SO, that should be sitting there pinging a Google ip. Let's pop out of screen for a minute. To leave a screen session on purpose and let it keep doing its thing, you hit the combination of 'ctrl-a d'. That is, hold down the ctrl key while hitting 'a', then let up and hit 'd' (for disconnect).

Code:
ctrl-a d
You should see something like:
Code:
[detached]
Now, you're back in your normal terminal. If not, try the ctrl-a d command again. Then, exit out so you're back on your PC again.

ssh back into your server and list the active screen sessions by typing:
Code:
screen -ls
You should see something like:
Code:
[[email protected] ~]# screen -ls
There is a screen on:
    24603.test1    (Detached)
1 Socket in /var/run/screen/S-rob.
Since there is only one active, you can re-connect to it by simply typing:
Code:
screen -r
Now, you should see your google ping still going. Hit ctrl-c to end it out.

That screen session would have kept going until you logged back in and stopped it, or if the server went down for some reason.

Now, disconnect from that screen session (ctrl-a d) and start a new one with:
Code:
screen -S test2
Once in there, disconnect (ctrl-a d) and list out the active sessions:
Code:
screen -ls
You should see something like:
Code:
[[email protected] ~]# screen -ls
There are screens on:
    25439.test2    (Detached)
    24603.test1    (Detached)
2 Sockets in /var/run/screen/S-rob.
Typing 'screen -r' won't work here because it wants you to specify which screen to connect to:
Code:
[[email protected] ~]# screen -r
There are several suitable screens on:
    25439.test2    (Detached)
    24603.test1    (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
So, we need to do it like this:
Code:
screen -r test1
Boom, you're back in your original (test1) session!

To kill your sessions, you can reconnect to them, then simply type 'exit' and it will close the session returning you to your normal prompt.

Hope this helps! screen is something I'll use most throughout the day. You can find more information on screen here: https://www.gnu.org/software/screen/
 


JasKinasis

Well-Known Member
Credits
4,005
Nice, tutorial.
I used to use screen all the time. But a few years ago I switched to using tmux. For the life of me, I can't remember why I stopped using screen. But screen and tmux are pretty much the same thing!
 

Rob

Administrator
Staff member
Credits
129
Nice, tutorial.
I used to use screen all the time. But a few years ago I switched to using tmux. For the life of me, I can't remember why I stopped using screen. But screen and tmux are pretty much the same thing!
Thanks!

Yeah, i've used screen forever it seems, but just w/ the basics. I haven't looked into multiple terminals within screen or splitting screen or anything. I've looked into tmux, but since screen is installed already on most of my servers already I just stuck with it instead of learning it.
 

JasKinasis

Well-Known Member
Credits
4,005
Thanks!

Yeah, i've used screen forever it seems, but just w/ the basics. I haven't looked into multiple terminals within screen or splitting screen or anything. I've looked into tmux, but since screen is installed already on most of my servers already I just stuck with it instead of learning it.
Fair do's!

I used screen for quite a while too. I think the main reason I switched to tmux was because the syntax for configuring and scripting it was much simpler. Another annoying factor was that screen couldn't do vertical splits. That situation may have changed by now - IDK.

I have scripts set up that run tmux sessions with different panes/window layouts for different types of projects/uses. Including some split panes.

After being a screen user for so long, the very first thing I did with tmux was change the default leader key from ctrl-B to ctrl-A! XD
 

Rob

Administrator
Staff member
Credits
129
For those interested, throw this in ~/.screenrc to make a cool status bar within your screen terminal showing hostname, time/date and load:
Code:
caption     always        "%{+b rk}%H%{gk} |%c %{yk}%m.%d.%Y | %72=Load: %l %{wk}"
hardstatus alwayslastline "%?%{yk}%-Lw%?%{wb}%n*%f %t%?(%u)%?%?%{yk}%+Lw%?"
 


Members online


Top