Bash – Dump process’s stdin and stdout

bashcommand linelinuxshellshell-script

I have two processes, let's say Parent and Child. Parent launches the Child and communicates with it through child's stdin and stdout.

Parent <-> Child

These processes use text protocol and I need to investigate it. I would like to create a bash script which will be launched by the Parent instead of child. This script will launch the Child and in addition will dump stdin and stdout streams to a log files.

Parent <-> MyProcess <-> Child
            |
            v
          log.txt

Is there a way in bash to do what I need or do I need to use C?

Best Answer

The simplest approach would be to use tee to dump the input to and output from the child to two separate files like so:

#!/bin/bash
tee in.log | child | tee out.log

You could use tee's -a parameter (append) to write both logs to the same file, but I'm not quite sure if they'll be interleaved in the right order or just written one after the other:

#!/bin/bash
tee -a both.log | child | tee -a both.log
Related Question