5

I have a couple of questions regarding screen: When I type screen -r, I get the following:

There are several suitable screens on:
    25154.tracks    (Detached)
    29278.mywork    (Detached)
    29138.mywork    (Detached)
    30915.mywork    (Detached)
    20065.mywork    (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

I'm not sure, but I believe most of these screens are old, since I haven't used screen in some time, and I just accidentally detached from one. With this, my questions are:

  • Is there a way to resume the most recently detached screen?
  • How do I "delete" older screen sessions?
  • Is it possible to display the date and # of screens within each session?
  • Is there a way to temporally associate aliases to the screens listed by screen -r to facilitate their choice? For example, it would be great if screen -r listed screens as follows:
There are several suitable screens on:
[1] 25154.tracks  (Detached)
[2] 29278.mywork  (Detached)
[3] 29138.mywork  (Detached)
[4] 30915.mywork  (Detached)
[5] 20065.mywork  (Detached)
Choose  one to resume:

And then I could just type 1,2,3,4 or 5 without having to type the full pid of the screen I want to resume. Is there anything that would do the trick?

2 Answers 2

3

Here's a script that should work for you.

#!/bin/bash

function chooser {

    echo
    echo "I found the following screen sessions: "
    echo

    pcount=0

    # 
    # find the session dir
    #
    sessdir=$( screen -ls | egrep 'Socket' | awk '{print $NF}' | sed -e 's/\.$//' )

    #
    # enumerate existing sessions, and add them to the plist() array.
    #
    for screen in $( find $sessdir -type p ); do
            pcount=$((pcount+1))
            pname=$( basename $screen )
            pdate=$( ls -latr $screen | awk '{print "( "$6" "$7" "$8" )"}')
            plist[$pcount]=${pname}
            echo "  [$pcount]       $pname   $pdate"
    done

    echo
    echo -n "Please select a session to reconnect to: "
    read choice

    # 
    # if the selected choice doesn't exist, recycle the chooser.
    #
    if [ -z ${plist[$choice]} ]; then
            echo
            echo "Your choice [$choice] is invalid.  Please try again."
            echo
            sleep 1
            chooser
    else
            screen -r -d ${plist[$choice]}
    fi

}

#
# the chooser function does all the work
#
chooser

I didn't distinguish between sessions that are currently attached or detached, so you may have to do that yourself if it matters to you.

5
  • 2
    Better to get the session directory from screen -ls; for example, on Ubuntu 11.10, the dir is /var/run/screen/S-$(whoami).
    – Arcege
    Oct 18, 2011 at 0:35
  • edited to include suggestion from @Arcege. Oct 18, 2011 at 2:24
  • 1
    i'm actually using this myself now. :) Thanks for asking this question. Oct 19, 2011 at 17:31
  • You can combine grep + awk like this: awk '/Socket/ {print $NF}'.
    – janmoesen
    Nov 1, 2011 at 22:26
  • select might come in handy for the menu, even though its output format is not that nice.
    – janmoesen
    Nov 1, 2011 at 22:27
2

The first thing to do is to determine the location of the session directory. You can get that from the output of screen -ls.

# session directory
sessdir=`screen -ls | sed -ne 's/.*Sockets* in \(.*\)\.$/\1/p'`
# display age of sessions:
ls -l $sessdir
# newest session
newest=`ls -1t $sessdir | head -1`
# Kill all sessions but newest
ls -1t $sessdir| sed 1d | while read sess; do screen -m -S $sess -X quit; done

The "newest" session is the one most recently created; I do not believe there is any information kept about when a session was detached.

You could certainly make a wrapper to select from a list and start that session, but you are not able to retrieve information from an existing session (the output goes to the session, not to the output of the calling program.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .