1

Given a shell script (bash in this example), one can use the set -x or -x passed to bash or other supporting shells to generate debug output.

I know how to redirect said output to a file ./script.sh 2> out.log or bash -x script.sh 2> out.log.

But how to send stderr to a terminal multiplexer, like screen or tmux?

Mainly useful when script change the way the terminal is displayed(TUI), so sending stderr enable more comfortable debugging in another window/terminal.

1 Answer 1

2

Invoke tty in the terminal you want to send something to. It will print e.g. /dev/pts/3 Then redirect anything you want to this file, possibly from another terminal. Like this:

bash -x script.sh 2> /dev/pts/3

This works with tmux, screen, separate terminal emulators or ttys (where you are logged in). Invoke tty in one shell and redirect/print to whatever it says from the other.

I use similar approach to debug my pipelines. Example:

foo | tee /dev/pts2 | filter1 | tee /dev/pts3 | filter2

tmux command display '#{pane_tty}' will print /dev/pts/3 or so in the status line. This is useful if there's no shell in the target pane (so you cannot easily run tty there). Select the pane so it's current, hit prefix:, type the command and Enter.

4
  • so i just do cat /dev/pts/3 in a loop, inside a tmux/screen session? (need to continuously see the debug output) Oct 31, 2020 at 20:25
  • Thanks for the edit on your post. Seems to work :), also found that i could use ps ax | grep $$ | awk '{ print $2 }' inside the wanted pane in tmux, and then pipe to it like in your example. (feel free to add this method to your answer too) Oct 31, 2020 at 20:29
  • 1
    @NordineLotfi Don't cat. Spawn two terminals, two shells; it doesn't matter if with tmux, screen, separate terminal emulators or ttys. Invoke tty in one and redirect/print to whatever it says from the other. Oct 31, 2020 at 20:29
  • yeah, forgot that screen/tmux spawn their own shell instance :D. Got it. Oct 31, 2020 at 20:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .