27

I just want to have a quick overview of my detached GNU screen sessions. Is it possible to dump the current content to stdout ? Something like

screen --print SESSION_NAME > file.txt
George M
  • 13,959
Pierre
  • 1,793

3 Answers3

23

You can dump one window of a screen session with screen -X hardcopy /some/file, that will save a screen dump of the current window in /some/file.

You can dump a specific window with:

screen -X at 3 hardcopy /some/file

And all of them in a single file with:

screen -X eval 'hardcopy_append on' 'at \\# hardcopy /some/file'

You can also dump one file per window, by specifying which directory to dump them in (if you don't specify it, then they'll be dumped in the directory screen was started in) with

screen -X eval 'hardcopydir /some/dir' 'at \\# hardcopy'

(will be dumped in files called hardcopy.<n>)

See the -h option of hardcopy to include the scroll buffer.

Use screen's -S option as usual to specify the session to run the command in. ⁣   ⁣    ⁣ ⁣

  • 4
    If the screen session has never been attached, selecting a window with -p or at is necessary. So if screen -X hardcopy /some/file doesn't work, try screen -X at 0 hardcopy /some/file or screen -p 0 -X hardcopy /some/file. – Gilles 'SO- stop being evil' Apr 12 '13 at 22:42
  • 4
    Is there any way to hardcopy to stdout outside of outputting to a temporary file, catting it, then deleting the temporary file? – Claudiu May 04 '15 at 17:32
  • 4
    @Claudiu: Your question is 3y+ old but the answer is relevant anyway: no; I just figured out using strace that the "client" screen sends the hardcopy command to the server and the server is what processes it. (The manpage states the hardcopy file is written in the server's cwd, not the client's.) The TTY data apparently does not go over the wire in the case of hardcopy. – i336_ Jun 05 '18 at 02:58
15

Start screen with the -L option to enable logging of the session.

Alternatively, after you have launched screen, you can turn logging of a window in the screen session on and off with CTRLa-H.

The logs will be created in your current working directory with the name screenlog.X where X is a unique number.

The output of the windowlist when logging is enabled has an L in the Flags column.

Num Name           Flags
  0 fedora         $(L)               
  1 sudo           $                                        
  2 fedora         $(L)                                                
George M
  • 13,959
5

Using named pipes, you can output to stdout, like the title (not the question) asks.

function screen_peek()
{
  # Create a random filename
  local pipe="$(mktemp -u)"
  # Crete a named pipe
  mkfifo "${pipe}"
  # Runs hardcopy in background, wrt bash (the client)
  screen -S "${1}" -X hardcopy "${pipe}"
  # Redirect pipe to stdout and remove trailing and leading blank lines
  cat "${pipe}" | sed -e '/./,$!d' -e :a -e '/^\n*$/{$d;N;};/\n$/ba'
  # Cleanup
  rm -f "${pipe}"
}

Anonymous pipes (and coproc) in bash won't cause cat to end (EOF) when the screen hardcopy dump finishes, because it keeps the pipe open. An anonymous pipe pair should detach in other languages, such as low level calls in python.

Andy
  • 202