0

I'm using screen to monitor several parallel jobs to test small variations of my program. I gave each screen session a different logfile. I do not remember which logfile I set for which session, and now wish I did!

Is there a way to query which session name (usually of the form #####.ttys000N.hostname) goes with which logfile, or vice-versa?


(To whom it concerns: the tag suggests determining which SX site the question is most relevant to. Based on the help pages of SuperUser and StackOverflow, this question appears roughly equally applicable to either community. Feel free to migrate it if you think it belongs elsewhere.)
jvriesem
  • 1,859
  • 3
  • 18
  • 40
  • One way is to run `screen -r` to list active screen sessions, and then resume one of the open screen sessions by name. Then, Ctrl-a : and type `logfile` and hit Enter. Doing this without an argument will tell which logfile is associated with the screen session you just resumed, and because you used the session name to resume the session, you're good. I'm looking for a less paper-and-pen way to do this. – jvriesem Jun 19 '18 at 18:41
  • 1
    From `screen -ls` you have the list of process ids, and can then do an `lsof -p ` on each pid to find which files it has open, of which the log file. You may need `sudo` for the `lsof`. – meuh Jun 20 '18 at 17:01
  • @meuh: Although your solution is not intrinsic to `screen`, it works. If you use that as an answer, I'll accept it unless/until something better comes along! – jvriesem Aug 24 '18 at 18:07

1 Answers1

1

I didn't find my suggested comment of using screen -ls to list the process ids, and then doing an lsof -p on these to find the filenames very satisfactory, so here is another not entirely satisfactory alternative:

There is an option -X to send commands to a remote screen, but unfortunately any output is shown on the remote. There is an option -Q to send a command and print the result locally, but it only accepts a very limited set of commands. However, one of these is lastmsg, which repeats the last message displayed.

So you can use -X logfile to display the name of the logfile remotely, then immediately use -Q lastmsg to duplicate that display locally! There is, of course, the possibility of some event occurring in the middle of this non-atomic action. The two commands cannot be combined. Here's an example:

#!/bin/bash
screen -ls |
while read session rest
do  if [[ "$session" =~ [0-9]+\..+ ]]
    then  screen -S "$session" -X logfile # shows in status
          msg=$(screen -S "$session" -Q lastmsg)
          # logfile is '/tmp/xxxxx'
          echo "$session $msg"
    fi
done

and some typical output:

21017.test2 logfile is '/tmp/xxxxx'
20166.test logfile is '/tmp/mylog.%n'
meuh
  • 11,500
  • 2
  • 29
  • 45