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.0What does it mean
-dmstarts the new screen session while remaining detached.-Sdefines the session name.-Lenables logging to a file at 10 seconds intervals.-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.0is realtime.^Msends a CTRL + M key combination to the session. Otherwise you have only typed the command without hitting enter.
For the tail command:
-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 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 errorNote
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
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?
Hi, thanks! You can write the current screen buffer to a file with the
hardcopycommand: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
scrollbackcommand:screen -S test -X colon 'scrollback 5000^M'Thx 2 u! You saved my day!
how do you create multiple screen log file if you were to run multiple screen session
The
0at 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"