0

Does someone happen to know if it is possible to print two echo commands of a script to the same terminal? The problem is that the script that should print to the terminal is automatically executed by a systemd service.

I already found out that it is possible to open a terminal and let it execute some commands like this:

konsole -e command

However, this will open a new terminal for every command and i want them all to be executed in the same terminal.

Kind regards Pepsilon

  • maybe redirect to the `tty`. like `echo hello > $(tty)`. Untested suggestion – P.... Oct 29 '19 at 16:17
  • put what you need in a script and the use `konsole -e /path/to/myScript` ? OR maybe you can inline it with `konsole -e 'echo one ; echo two'` ? Good luck. – shellter Oct 29 '19 at 16:20

3 Answers3

0

The problem is that the script that should print to the terminal is automatically executed by a systemd service.

...and, because of that, there is not a terminal associated: usually, system services write to log files, not terminals.

But, you can redirect echoes to console (/dev/console), which is intended to be the only terminal "always present". Otherwise, you can redirect to any terminal you know is there - I mean things such /dev/ttyS0 or similar, if you know what you are doing (and your script has permission to do so). In a graphical environment there can be anything, for example my system has about 4 text terminals that can be viewed by pressing Ctrl-Alt-F1/2/3 and so on.

0

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.

dash-o
  • 13,723
  • 1
  • 10
  • 37
0

i propose using a named pipe created with mktemp in space for temporary files. Note that mktemp -u is slightly unsafe.

i was helping myself with How to avoid echo closing FIFO named pipes? - Funny behavior of Unix FIFOs with that tail -f:

our_named_pipe=$(mktemp -u) &&
mkfifo $our_named_pipe && {
    tail -f > $our_named_pipe & # is holding pipe open
    named_pipe_holder_pid=$!
    echo $our_named_pipe # outputting in case you would want to unsafely use it outside 
    mate-terminal -e \
    "bash -c \"cat $our_named_pipe; echo 'Finished, press enter to exit'; read\"" && {
        echo "Do things, everythings fine" > $our_named_pipe
        sleep 10 > $our_named_pipe
    } || echo "You got an error from the terminal emulator"
    kill -9 $named_pipe_holder_pid
} || echo "you got an error from mkfifo"
Mika Feiler
  • 474
  • 3
  • 17