Putting a side the question on how to handle situations when there is no terminal/X session that can be used to capture the log. Those can be addressed by testing for X/terminal availability, and falling back to log file.
Each terminal is connected to unique pseudo-terminal (PTY), usually /dev/pts/0, /dev/pts/1, ..., . Whatever is send to the PTY will be displayed on the terminal (konsole, gnome-terminal, xterm, ...).
You can build a solution using the following
- Launch the terminal with a command:
konsole -e '(tty ; echo $$) > /tmp/active_tty ; while true ; do sleep 600 ; date ; done' &
.
- Effectively forcing the terminal to stay up till closed/killed.
- The file will capture tty name, and PID
- can use gnome-terminal, xterm, ...
- Each process that want to send output to the 'current' log terminal should redirect stdout and/or stderr to the one named in the file:
exec > $(head -1 /tmp/active_tty) 2>&1
or similar
The PID line can be used to implement a check if terminal PID still running, if needed.