When automating terminal sessions with screen
, the default logging behavior can create ambiguity when multiple instances run simultaneously. The command:
screen -L tree
Produces output in an unpredictable screenlog.N
file, where N increments starting from 0. This becomes problematic when:
- Multiple scripts run concurrently
- You need to process the output programmatically
- Different sessions require separate log analysis
Screen actually provides a built-in solution through its command-line interface. The -Logfile
parameter allows specifying an exact filename:
screen -L -Logfile custom_log.txt tree
This creates several advantages:
- Predictable log file naming
- No interference with other screen sessions
- No need for .screenrc modifications
For more complex logging requirements, you can create a temporary configuration:
cat > temp_screenrc <
The $$
ensures unique filenames by using the PID, while logfile flush 1
forces immediate writing.
Here's a complete Bash implementation for reliable logging:
#!/bin/bash
LOG_FILE="${1:-screen_output.log}"
COMMAND="${@:2}"
cleanup() {
[[ -f "$LOG_FILE" ]] && less -R "$LOG_FILE"
}
trap cleanup EXIT
if screen -L -Logfile "$LOG_FILE" -dmS "auto_log_$$" $COMMAND; then
screen -r "auto_log_$$"
else
echo "Failed to start screen session" >&2
exit 1
fi
Usage:
./logger.sh myapp.log python3 -m myapp --verbose
- Permission issues may arise when writing to certain directories
- The logfile inherits screen's encoding (typically UTF-8)
- ANSI colors are preserved only if the terminal supports them
- For production systems, implement log rotation
When using GNU Screen's logging feature with screen -L
, the output gets written to a generic screenlog.X
file (where X is a number). This becomes problematic when:
- Multiple screen sessions run simultaneously
- You need to programmatically identify your specific log
- You can't modify the user's
.screenrc
configuration
Screen actually provides a built-in way to specify log filenames without touching .screenrc
:
screen -L -Logfile custom_name.log command_to_run
Example with tree command:
screen -L -Logfile tree_output.log tree -C
For more complex scenarios, you can combine this with other screen options:
1. Timestamped log files:
screen -L -Logfile "output_$(date +%Y%m%d_%H%M%S).log" ./script.sh
2. Process-specific logging:
SCREEN_LOG="process_$$.log"
screen -L -Logfile "$SCREEN_LOG" python3 worker.py
To properly view the ANSI-colored output stored in your custom log file:
less -R custom_name.log
Or for real-time monitoring:
tail -f custom_name.log | less -R
If you need to enable logging after screen starts, you can use screen's command mode:
# Start screen normally
screen
# Then inside screen (Ctrl+a : to enter command mode)
:logfile custom.log
:log on
This approach works when you need to dynamically enable logging during a session.
- The
-Logfile
option requires GNU Screen version 4.00 or later - Relative paths are resolved from where screen was launched
- Log files accumulate - implement rotation if needed
- For scripting, always check if screen executed successfully