Find that lost screen session
Posted Thu, 05 Jun 2008
(*) 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:
- Bad: Kill the vim session that's editing the file, and rerun vim somewhere else.
- Good: Use xdotool to search window titles for 'foo.c'
- Great: Find the screen STY variable for the process 'vim foo.c'
- Great: Ask each open screen session about what it is on screen
- 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
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.
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...