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
  4. Related stuff

How to

Use -L to enable logging to screenlog.0. Then use -Logfile to override the file name.

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.

screen -dmS test -L -Logfile my.log
screen -S test -X colon "logfile flush 0^M"

# optionally go to session
screen -r test

# or follow the log 😉
tail -Fn 0 my.log

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.
  • -Logfile defines the file name to log to.
  • -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 grc for improved readability, or use grep to only display the lines you are interested in.

# watch for errors and colorize them
grc tail -Fn 0 my.log | 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 my.log but can also make the filesize huge and complicate parsing the file in scripts.

Categories: Code

Leave a Reply

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