Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
1  
Do you know about tee? –  n.st Feb 15 at 15:26
add comment

2 Answers

up vote 4 down vote accepted

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
share|improve this answer
    
Great code! I'm impressed. This is nearly what I need. On some reason child process exits immediately in this situation. I guess that's because parent is writing to a stdout of bash, but not stdout of tee. I do not know how to check this. –  Anthony Ananich Feb 15 at 19:33
    
This shouldn't change the behavior of the child process at all... Does the parent process pass any arguments to the child? If so, did you add them to the child invocation in the script? Another reason I can imagine is that tee might be buffering the stdin/stdout until it receives a newline character, thereby tripping some kind of timeout in the child process (though that seems unlikely, since you say the child is terminating immediately). –  n.st Feb 15 at 20:33
    
Sorry, it was a mistake of mine. This script works like a charm. –  Anthony Ananich Feb 17 at 8:44
add comment

If you use named pipes, then it doesn't matter if you have child and parent processes:

p1:

#!/bin/sh
# p1

rm -f p1.fifo;mkfifo p1.fifo
trap "exit 1"  0 1 2 3 13 15

while read line; do
    echo p1 got "$line"
    echo p1 sending $line to p2
    echo $line > p2.fifo
    sleep 1
done < p1.fifo

p2:

#!/bin/sh
# p2

rm -f p2.fifo;mkfifo p2.fifo
trap "exit 1"  0 1 2 3 13 15

while read line; do
    echo p2 got "$line"
    echo p2 sending $line to p1
    echo $line > p1.fifo
    sleep 1
done < p2.fifo

inital message:

echo message > p1.fifo

the output of p1:

p1 got message
p1 sending message to p2
p1 got message
p1 sending message to p2

the output of p2:

p2 got message
p2 sending message to p1
p2 got message
p2 sending message to p1
p2 got message
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.