How to save the output of screen windows to a file on Linux?

Logging screen output to a file

The screen terminal multiplexer can capture window output in several ways. Here are the most practical approaches depending on your use case.

Enable logging before starting screen

Start a new screen session with the -L flag to log all output to a file:

screen -L -S mysession

This creates a log file named screenlog.0 in your current directory. If you start multiple sessions, subsequent logs become screenlog.1, screenlog.2, etc.

To specify a custom log filename and location, use -Logfile:

screen -L -Logfile /var/log/myapp.log -S mysession

Enable logging in an existing session

If you’re already inside a screen session, toggle logging with Ctrl+A followed by H:

Ctrl+A H

You’ll see a message like “Creating logfile ‘/path/to/screenlog.0′”. Press the same keys again to stop logging.

Configure logging in ~/.screenrc

Add persistent logging configuration to your screen initialization file:

logfile /var/log/screen/%S.log
deflog on

The %S variable expands to your session name. Use %t for timestamp or %Y-%m-%d_%H-%M-%S for date-based filenames.

Create the log directory first:

mkdir -p /var/log/screen
chmod 700 /var/log/screen

Log specific windows only

To log only certain windows, toggle logging per-window. Enter a screen window and press Ctrl+A H to start/stop logging for just that window.

Rotation and cleanup

Screen doesn’t handle log rotation automatically. Use logrotate to manage large log files:

cat > /etc/logrotate.d/screen <<EOF
/var/log/screen/*.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
}
EOF

Alternative: Use tmux with logging

If you’re considering tmux instead, capture pane output to a file:

tmux capture-pane -p -S -30 -t sessionname > /tmp/pane_output.txt

This captures the last 30 lines. Use -S - to get the entire scrollback buffer.

For continuous logging with tmux, enable the logging plugin or script periodic captures via cron or systemd timer:

#!/bin/bash
SESSION="mysession"
WINDOW=0
LOG_DIR="/var/log/tmux"

mkdir -p "$LOG_DIR"
tmux capture-pane -p -t "${SESSION}:${WINDOW}" >> "${LOG_DIR}/${SESSION}-${WINDOW}.log"

Run via cron every minute:

* * * * * /usr/local/bin/tmux-logger.sh

Checking log permissions and access

Ensure your logs are readable by the appropriate users:

ls -l /var/log/screen/
cat /var/log/screen/mysession.log
tail -f /var/log/screen/mysession.log

If running screen under a unprivileged user, adjust directory ownership accordingly to avoid permission issues.

Performance considerations

Logging adds minor disk I/O overhead but is negligible on modern systems. For high-throughput applications, monitor disk usage:

du -sh /var/log/screen/

Screen buffers output in memory before writing, so even rapid output doesn’t cause excessive writes. If you need real-time, line-by-line capture, pipe screen’s output to external tools like tee instead:

screen -S mysession -d -m bash -c "mycommand | tee /var/log/output.log"

This runs the command in detached mode and logs all output while displaying it in the terminal.

Similar Posts

Leave a Reply

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