Bash – A way to distinguish between interleaved output from two background processes

bashjob-controlshell

If I have two backgrounded processes that produce input to STDOUT or STDERR (e.g., two installation scripts), is there an easy way to make these two output streams distinguishable? I guess I can pipe each processes's output through a sed program that prefixes every line of each output with a different tag, but I'm looking for something easier.

Best Answer

The easiest solution would be to start each of the two background jobs and redirect their output to files:

 utility1 >utility1.out 2>utility1.err &
 utility2 >utility2.out 2>utility2.err &

This has the added benefit of not clogging up your terminal with output.

You may obviously redirect both the error and output streams to the same file too:

 utility1 >utility1.out 2>&1 &
 utility2 >utility2.out 2>&1 &

You could also use tmux:

 tmux new "utility1" ';' split "utility2"

tmux will exit as soon as all commands have exited. To avoid that, change "utility" to "utility;read". This will make the pane stay open until you press Enter.

Related Question