How to have the screen command log its output in realtime to a file. Purely command line, no .screenrc file involved.
-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
-dmstarts the new screen session while remaining detached.
-Sdefines the session name.
-Lenables logging to a file at 10 seconds intervals.
-Logfiledefines the file name to log to.
-Xsends a command to an already existing session.
colontells the session an internal command is coming up.
logfile flushtells the screen to set the log interval time in seconds.
^Msends a CTRL + M key combination to the session. Otherwise you have only typed the command without hitting enter.
-Fsets tail to keep watching the file for changes.
-n 0means 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
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.