1

I have to run two processes parallel and continously process the output of them.

The basic execution should be started in a bash script which executes the python scripts as follows:

python somefile.py &
python someotherfile.py &

Both of them run for a long time and produce log outputs. I want to process the outputs of both of them (it is not important which program generated the log output).

Is it possible to redirect the output to a function which processes it?

3
  • I think what you are asking for is a Named Pipe. Google should be able to help you with that. Commented Feb 15, 2017 at 14:03
  • The main question is : do you need to process the output of both in the same process (i.e. if there is interaction between both) or can you process both outputs separately? The second case is quite simple, the first would involve named pipes or file descriptors (which can act like named pipes, without the creation of files). Commented Feb 15, 2017 at 14:44
  • Sorry I don't get what you mean by the first case? Commented Feb 15, 2017 at 15:01

1 Answer 1

1

This is the simplest thing that comes to my mind.

File a.sh:

while true; do
  echo A
  sleep 0.5
done

File b.sh:

while true; do
  echo B
  sleep 1.2
done

File c.sh:

(
./a.sh &
./b.sh &
) | while read output; do
  echo "C: read $output"
done

The file c.sh launches a.sh and b.sh, both in background, and reads the output from both. When c.sh runs, the output is:

C: read A
C: read B
C: read A
C: read A
C: read B
C: read A
C: read A
C: read B
C: read A
C: read A

The trick is the use of parentheses to group commands in a single unit (well, another shell). Those commands are run (in background in this example), and the overall stdout is piped into the while loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.