I use GNU screen mostly to run commands that become non-interactive after some initial input.

I do not like navigating output in screen sessions, I feel it is a bit buggy.

Is there a way to access the history of a screen session without screen -r? (Not sure about the correct terminology here, I simply mean all the input and output that has occured in the screen session, not the history in the sense of the commands that were entered in the shell prompt).

Ideally, I'd like to do this with (1) bash/Linux builtins, (2) else with screen itself, (3) else with a Python package, (4) and only if none of these are possible with an external program.

(1: e.g. cat /run/screen/S-user/12345.pts-1.pc 2: e.g. screen --cat 12345 3: e.g. pip install screen; python -c 'import screen;print(screen.read_socket("12345"))' 4: e.g.sudo apt install screenreader; screenreader 12345)

  • 1
    You can log all output to a file by starting with screen -L or setting log on, default file is ./screenlog.%n where %n is the window number. – meuh Aug 25 '18 at 17:56
  • @meuh I actually saw that before but if I start two screen sessions at the same time they ended up writing into the same file. Now that you said 'default file' I did some looking up and found out that newer versions of screen have 'Logfile' to change the file. That however would be a major annoyance to set by hand everytime and then delete the files afterwards. (I usually have quite a few screens running at the same time). Too bad screen doesn't set the default file name to the same file as the session itself (and deletes it afterwards...) – Bananach Aug 25 '18 at 20:24
  • 1
    You can set the log filename once (in ~/.screenrc) to something including magic characters like %S and it will be replaced by the session name, %t for the window title etc. You can even use the time and date. If you place them in /tmp/ most distributions these days will remove them after a few days (or when you reboot), or you can write your own cron job. – meuh Aug 25 '18 at 21:20
  • @meuh perfect. For anyone else that just wants the filename to look exactly as what is shown by screen -ls: I am now using Logfile ./%p.%S – Bananach Aug 26 '18 at 6:28

expect can log a session transaction and can go unattended after some initial interaction input. This will allow you to run a command, interact with it, then type ~. to hand things over to expect until the program exits. Meanwhile, the out.1535209800 or whatever can be inspected.

#!/usr/bin/env expect
log_file out.[clock seconds]
spawn -noecho sh  ;# here be the command to run
interact {
  ~. { return }   ;# type this when done interacting
  ~~ { send "~" } ;# or this to get a literal ~ in the shell
if {[catch {
  expect {
    eof { exit }
    match_max { exp_continue }
    timeout { exp_continue }
} oops ]} { puts stderr $oops; exit 1 }
  • Nice to know. Unfortunately, I cannot guarantee no input happening anymore though, it's just what happens most of the time: I open screen, have a look at what the program did in the last few hours and close it again. If some bigger step is done, I may or may not take further steps based on the results. Also, I like not having to deal with disown and its particularities – Bananach Aug 25 '18 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.