How to write screen output to a log file

How to have the screen command log its output in realtime to a file. Purely command line, no .screenrc file involved.

  1. How to
  2. What does it mean
  3. Note

How to

Use -L to enable logging to screenlog.0. By default the screen output is dumped to the log at a 10 second interval. To change this you name your session with -S and after creation send the logfile flush command with the number of seconds.

# Start session with logging
screen -dmS test -L

# Enable realtime logging
screen -S test -X colon "logfile flush 0^M"

# Go to session and do your thing
screen -r test

# Or follow the log
tail -Fn 0 screenlog.0

What does it mean

  • -dm starts the new screen session while remaining detached.
  • -S defines the session name.
  • -L enables logging to a file at 10 seconds intervals.
  • -X sends a command to an already existing session.
  • colon tells the session an internal command is coming up.
  • logfile flush tells the screen to set the log interval time in seconds. 0 is realtime.
  • ^M sends a CTRL + M key combination to the session. Otherwise you have only typed the command without hitting enter.

For the tail command:

  • -F sets tail to keep watching the file for changes.
  • -n 0 means to only watch, don’t output older lines.

When tailing the log you can combine more magic, like parsing the output with colorizer ccze for improved readability, or use grep to only display the lines you are interested in.

# watch for errors and colorize them
tail -Fn 0 screenlog.0 | ccze | grep -i error

Note

One thing to keep in mind though: all screen output is being written to the file. When a running command inside the screen session is inline updating lines, by using the \r or carriage return character to make it look like an animation, all of those changes including the erased characters are being saved in the log file. That allows you to completely replay a session using cat screenlog.0 but can also make the filesize huge and complicate parsing the file in scripts.

Like this article?
Buy me a coffee

Changelog
2021-09-08 – The Logfile option is no longer working


Related stuff


Comments

5 responses to “How to write screen output to a log file”

  1. Anonymous

    Great write up, thanks, but what if it’s a session that was started without logging, and I want to dump out all its console output so far into a file?

    1. Franklin

      Hi, thanks! You can write the current screen buffer to a file with the hardcopy command:

      screen -S test -X colon 'hardcopy my-screen.log^M'

      This is limited to only the size of the buffer. By default the last 100 lines. You can change the buffer size for a running session with the scrollback command:

      screen -S test -X colon 'scrollback 5000^M'

  2. Anonymous

    Thx 2 u! You saved my day!

  3. Anonymous

    how do you create multiple screen log file if you were to run multiple screen session

    1. Franklin

      The 0 at the end of the filename is the terminal ID from where the commands are run. When you run the command from a second (simultaneous) terminal session the ID will increase. Not to be confused with screen sessions, there needs to be a second terminal window open.

      Starting from about screen v4, you can also override the filename by adding this command to those in the article:

      screen -S test colon "logfile my-test.log^M"

Leave a Reply

Your email address will not be published. Required fields are marked *