Jump to content

Screen


Steve Scrimpshire
 Share

Recommended Posts

screentute.png

One program in Linux that I have found very, very useful is 'screen'. It can do amazing things. I'm sure there are people out there more experienced and knowledgable about screen and their input in this topic is welcome, but I thought I would help alleviate some of the learning curve by giving input on screen while I am still learning.

screen is very difficult to grasp at first, very confusing. But let's start from the beginning. When you open a terminal-emulator (I will refer to it the rest of the time as a 'console' even though that term is misused in this sense) in Linux and start a process, it dies with the console when you log out of it. There are exceptions (processes that run as daemons or services and probably others), but we will pretend they don't exist in this 'tutorial'. Even sending a process to the background by using 'process &', the process will still die when you log out of that console.

Along comes screen to help us out in situations where we'd like to keep the process running, but still close the console. How do we manage all this? screen allows you to detach, and reattach later, screen 'sessions'. The very useful thing about this is evident mostly inside of ssh sessions. I can ssh into my desktop remotely and start a screen session, then I want to edit a file. So, I run vim myfile inside this screen session. Well, I'm not done editing the file, but I was ssh'ed in from work and now I need to go to the library and use the internet there. I simply detach the screen while my vim is still open, logout of ssh, and go to the library and ssh back in to my desktop and reattach screen. Lookie there, my vim is still open right there in front of me. How is this accomplished? Here's what the steps would look like:

 

ssh mydesktop

<<log in>>

screen

vim myfile

<<do some editing>>

<<uhoh, time to go>>

Ctrl-a d <<this key combination detaches the screen>>

logout

<<drive to the library>>

ssh mydesktop

screen -r <<reattaches the detached screen>>

<<oh no, what if I forgot to detach the screen when I left? Do the following instead>>

screen -d -r

<<continue editing my file>>

 

There are several methods of reattaching, but detaching, if necessary, first....read the man page to get an idea. For example, screen -D -r will detach the screen and logout remotely, if necessary.

What if I have more than one screen detached? We'll cover that when I continue.

Edited by Steve Scrimpshire
Link to comment
Share on other sites

I'm halfway through writing a short faq on screen...

 

it's the most revolutionary and genious program on my system :P

 

i remember the days when I could have as many as 10 console windows open. I'd start something in one, leave it, open a new one, do somethjing, but as they stacked up, i never went back to reuse them.... so at the end of a day I could have as much as 20 consoles open..!!

 

screen rocks and i will only ever have 1 console open. Ctrl-alt-t, opens a new gnome-terminal, and detatches screen wherever it is and attatches to my screen session :D

 

iphitus

Link to comment
Share on other sites

while in screen ctrl-a-w will list the current screen's open at the moment.

 

I use this often when compiling and doing sytem stuff.

as normal user

screen

then

ctrl-a :screen 9 su -

the : means pass a screen command

in this case I am opening a new screen session on screen 9 and auto start su -

Now at any time I can ctrl-a 9 to swap to my su session

Link to comment
Share on other sites

Let's look at what paul posted with a newbie's perspective:

 

I'm going to try to keep the terms "screen" and "window" separate for clarity's sake. The commands that paul just did are broke down like this:

 

Ctrl-a Gives you the ability to pass a command to screen.

: Screen command keybinding that enters commandline mode. Whatever you type after it will be run as a bash command.

So:

Ctrl-a :screen 9 su -

Enters commandline mode and passes screen 9 su - to screen. Technically, we are initiating another instance of screen inside of screen, but we will call that a new 'window', since that is how screen treats it. The first instance is window0 and the one we just passed becomes window9. We can toggle between them with Ctrl-a 0 and Ctrl-a 9. If you do Ctrl-a d to detach your screen session and then do screen -r you will be immediately attached to that one instance of screen with 2 windows. To list the active windows in your screen session, there are 3 ways:

Ctrl-a w

Ctrl-a Ctrl-w

Ctrl-a "

 

Ctrl-a w and Ctrl-a Ctrl-w are equivalent and in a console window, you will see the list of active windows in your title bar. If, instead, you ran screen from a virtual terminal (the ones you get to with the key combination of Ctrl-Alt-F1, Ctrl-Alt-F2, etc), you would see the window list in the very bottom of the screen (the same place you see keybindings commands echoed).

Ctrl-a " is a little different. It clears the screen (not literally, because the info is still there, just hidden) and shows you a list of windows in the console window itself or in the virtual terminal). You can highlight the window you want to jump to with the Up or Down arrow keys and hit enter.

Before we go to much further, let's see how we can get help while inside of screen.

Ctrl-a ? Will give you a list of all the keybindings screen understands and what they do.

 

More to follow.

Edited by Steve Scrimpshire
Link to comment
Share on other sites

Logging the output of your screen windows:

 

While in a window of screen, you can toggle the logging of that window by doing

 

Ctrl-a H

 

You'll see whether you just toggled it on or off by looking at the titlebar of you console window or the message line of you VT at the bottom. There is a drawback to this. It logs to screenlog.<<window#>> no matter which screen session you are in. So, for example, if you have two separate screen sessions and you are logging Window 1 in both of them, output from each will be appended to screenlog.1, possibly intermingling output. I have yet to find away around this. Even naming the windows, doesn't help. If any screen gurus knows the answer, please come forward.

 

Note: All screen commands are case-sensitive. Ctrl-a H is different from Ctrl-a h (although, I think if there is no command for Ctrl-a h, it will assume you meant Ctrl-a H).

Edited by Steve Scrimpshire
Link to comment
Share on other sites

Heres a usefull tip. to get a list of currently open windows in screen on the bottom of the terminal, ala:

 

http://aslan.no-ip.com/~iphitus/screenshots/screen.png

 

Add this line - it's one long line, to your ~/.screenrc

hardstatus alwayslastline "%{wk}%?%-Lw%?%{bw}%n*%f %t%?(%u)%?%{wk}%?%+Lw%= %{mk}@ %H %{ck}%Y%{wk}-%{ck}%m%{wk}-%{ck}%d %{yk}%D %{gk}%c"

 

If you want screen to startup apps when you start your screen session, add a line to your ~/.screenrc, ala:

screen irssi
screen ncmpc
screen bash

 

That will start each of those program in a window. If you launch them like that, the window is properly titled, like in my screenshot.

 

But maybe you already have a screen session open, to start irssi, type "screen irssi" and it runs irssi in a new window in your current session. The benefit of this is, that it properly titles the window, so if you're using that window list line, you can see which one is irssi.

 

Because I am at my terminal a lot, i use screen to notify me of things, and I have programs send messages to screen that appear on the bottom line for a moment.

 

screen -X echo "Someone is talking to you"

 

Thats what displays when someone sends me a message on msn. that text for a moment appears on the bottom of my terminal, in a similar fashion to the "New screen" message you get when starting screen.

 

To create a new window within the current screen session, do ctrl-a c, and bang! you've got a shiny new terminal. To move around different windows, ctrl-a n for next in list, ctrl-a p for previous in list, ctrl-a space is also next. Ctrl-a a wil swap to the last used screen.

 

My tip, make your shortcut to gnome terminal or aterm or whatever it may be, add this to the end of it "-e screen -D -R"

That will, look for an existing screen session, if it finds one, it will detatch it from whatever has it, and then attatch it to the current window. If it doesnt find one, it makes a new one. So wherever i am, i click my shortcut and my terminal window opens right where i left off.

 

Also of note, if you use screen its safe to start a program in the terminal, and then close the window , it'll keep running.

 

When i use the word screen, i mean the program. When i use the word session, i am talking about the group of windows within screen. and when i use the word window, i mean each individual program running inside the screen session, that can be swapped between, bash prompts included.

 

Some of the programs i use with screen,

mutt, email client

pebrot, console msn client

irssi, irc client

moc, music on console

ncmpc, console client for MPD music player.

 

Theres not much you cant do at the command line. The screenshot above was taken on the command line, ctrl-alt-f1.

:D

Edited by iphitus
Link to comment
Share on other sites

Side notes:

screen -X echo "Someone is talking to you"

When I try a command like this, I get "-X echo requires one or two arguments" echoed to my screen status line. If I just pass one word to 'echo' it works as expected. bash3, MDK10.1, Eterm and Fluxbox.

 

My tip, make your shortcut to gnome terminal or aterm or whatever it may be, add this to the end of it "-e screen -D -R"

This worked at one point, briefly. I do not know what I did to mess it up, but apparently it starts a process for the console and a different process for the screen and screen writes the socket with the PID of the console and not of screen, confusing screen. Even trying to start another screen window inside of screen, it looks for the socket where its supposed to be, but it's not there.

 

Busy lately at work...the howto will continue shortly...carry on.

Edited by Steve Scrimpshire
Link to comment
Share on other sites

I wasn't real crazy about iphitus's line:

hardstatus alwayslastline "%{wk}%?%-Lw%?%{bw}%n*%f %t%?(%u)%?%{wk}%?%+Lw%= %{mk}@ %H %{ck}%Y%{wk}-%{ck}%m%{wk}-%{ck}%d %{yk}%D %{gk}%c"

 

So, I played around with it a little bit (can't find any info on the web about the syntax of this) and came up with:

hardstatus alwayslastline "%{gk}%c %{yk}%D %{ck}%Y%{wk}-%{ck}%m%{wk}-%{ck}%d%= %{mk}%H %{wk}%?%-Lw%?%{bw}%n*%f %t%?(%u)%?%{wk}%?%+Lw"

 

Here's a 'screen'shot:

post-835-1109918535_thumb.jpg

 

Don't ask me what all the codes mean...I just copied and pasted. :D

Link to comment
Share on other sites

I wasn't real crazy about iphitus's line:
hardstatus alwayslastline "%{wk}%?%-Lw%?%{bw}%n*%f %t%?(%u)%?%{wk}%?%+Lw%= %{mk}@ %H %{ck}%Y%{wk}-%{ck}%m%{wk}-%{ck}%d %{yk}%D %{gk}%c"

 

So, I played around with it a little bit (can't find any info on the web about the syntax of this) and came up with:

haIrdstatus alwayslastline "%{gk}%c %{yk}%D %{ck}%Y%{wk}-%{ck}%m%{wk}-%{ck}%d%= %{mk}%H %{wk}%?%-Lw%?%{bw}%n*%f %t%?(%u)%?%{wk}%?%+Lw"

 

Here's a 'screen'shot:

post-835-1109918535_thumb.jpg

badum boom tshhhhh :drum:

 

Don't ask me what all the codes mean...I just copied and pasted. :D

 

Dont ask me what the line's syntax is... I just copied and pasted :P :D :banana:

 

I cant remember where it was I found it lol. but your modifications look good.

Link to comment
Share on other sites

  • 2 weeks later...

What if I have more than one screen session running?:

 

< omar ~ > screen -list

There are screens on:

        5439.pts-0.laptop      (Attached)

        19255.pts-2.laptop      (Attached)

2 Sockets in /home/omar/tmp.

 

I can do

 

screen -D -R 19255

 

to attach to the second session...to attach to the first:

screen -D -R 5439

 

You may find it useful to give your screen sessions meaningful names if you want to have more than one running at a time:

screen -S first

screen -S second

 

would've created in my first example:

< omar ~ > screen -list

There are screens on:

        5439.first      (Attached)

        19255.second      (Attached)

2 Sockets in /home/omar/tmp.

 

I could then do either

screen -D -R 5439

or

screen -D -R first

 

to attach to the first one.

Next we will discuss the -X switch. More fun.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...