How to have the screen command log its output in realtime to a file. Purely command line, no .screenrc file involved.
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.
Changelog
2021-09-08 – The Logfile option is no longer working
Leave a Reply