Search this site


Metadata

Articles

Projects

Presentations

Find that lost screen session

Scenario: I run lots of xterms. Each xterm runs a single screen session(*). At any given time, I can only see some of the xterm windows (the others are hidden).

(*) All my xterms run with: 'xterm -e screen -RR'. This causes them to attach to the first-found detached screen, and if none exist creates a new screen session. See run-xterm.sh for my pleasant, random-colored xterm script.

Problem: I forget where I put things. I can't find that terminal where I'm editing foo.c!

Possible Solutions:

  1. Bad: Kill the vim session that's editing the file, and rerun vim somewhere else.
  2. Good: Use xdotool to search window titles for 'foo.c'
  3. Great: Find the screen STY variable for the process 'vim foo.c'
  4. Great: Ask each open screen session about what it is on screen
Today, we'll cover the two 'great' solutions. I wrote both of these a while ago, but I totally forgot to post about them. Here you go :)

Find a screen by it's child processes
Tool: screenps.sh

This tool takes a regexp pattern as the only argument and will output a list of screen sessions having child process commands that match that pattern. This is useful for finding what screen is running 'vim foo.c'

% ./screenps.sh 'vim foo.c'
23464.pts-0.snack
Find a screen by what is being displayed
Tool: screen-find.sh

This tool takes a regexp pattern as the only argument. It uses screen's hardcopy command to save the on-screen buffer and then applies the regexp given to the buffer. If it matches, the screen session is output. There is special behavior if only one screen session is found: If the screen session is currently attached, it will flash that screen session giving you a visual clue about where it is; if it is not attached, it will attach to it.

% ./screen-find.sh "keynav"
28504.pts-27.snack
In case you still aren't clear, the two tools help you find your lost screen sessions. Maybe they aren't lost, but certainly it's easier to search for them by text than by eyeballs if you know what's in them.

A short summary: screenps.sh will search for commands running in a screen session and screen-find.sh will search for literal text displayed in a screen session. Both are super useful.

Note: Currently, screen-find.sh can only capture the contents of the 0th screen window (screen sessions can have multiple windows). I worked for a while on solving this, but for whatever reason I couldn't get it working properly.


3 responses to 'Find that lost screen session'

Showing last 3 comments... (Click here to view all comments)

Dan Fitch wrote at Fri Jun 6 05:36:41 2008...
Cool stuff.

What I do to make this work for screen sessions that are running over ssh from elsewhere is:

defhstatus ">^E0` ^Ew"
backtick 0 60 60 screentitle

(screentitle is a script which figures out what to "name" the current session... hostname, or hostname+pid)

Then there's a long hairy Ratpoison ruby script that searches for ">hostname" in the title, parses the "w" hardstatus output and ends with:

if window and window.screen_session
  screen = window.screen_session.detect { |x| regex.match x.title }
  exit 1 if window == current_window and screen.number == window.screen_session.number
  Ratpoison::ScreenSession.select screen.number if screen
end


This lets me have a "type to select window" that can cycle through xterms AND individual screen sub-windows, and also quick selects for things like "irssi on host X", "vim on host Y", etc...

Jordan Sissel wrote at Fri Jun 6 08:06:34 2008...
my .zshrc has this, which is run every time a command is run (or the prompt is echoed):

print -Pn "\ek${value}\e\\"  # screen title (in windowlist)
print -Pn "\e_${location}\e\\"  # screen location

'value' is given as the command or the prompt value. The 'location' is automatically set with the hostname of the machine. My xterm title, then becomes "[0] {hostname} - {command}"

So, if I have two machines, 'foo' and 'bar'. Foo is my workstation, bar is a remote server I ssh to. If I run screen locally and get a shell, the title is '[0] foo - zsh'. If I ssh to 'bar' and get a shell, my prompt changes to '[0] bar - zsh' assuming the same .zshrc file is present.

This lets me query for terminals to remote machines.

Matt Simmons wrote at Fri Jun 6 11:32:24 2008...
One thing that I really like about screen, and I didn't realize until I accidentally tried it, was that you don't have to give the full name of the screen session to restore. You only have to give as much as will uniquely identify it.

I'm sure you probably knew that, but I was clueless about it, so maybe it will help someone else.


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment: