views:

91

answers:

2

I am writting a shell script and i want these commands to run at the same time

find ./incoming/kontraktor/ -type f -name '*.html' | sort | awk 'NR % 3 == 1' | ./bin/foo.py -m 3 -b 1 | next_command >> log/foo_log.log 2>&1
find ./incoming/kontraktor/ -type f -name '*.html' | sort | awk 'NR % 3 == 2' | ./bin/foo.py -m 3 -b 2 | next_command >> log/foo_log.log 2>&1
find ./incoming/kontraktor/ -type f -name '*.html' | sort | awk 'NR % 3 == 0' | ./bin/foo.py -m 3 -b 3 | next_command  >> log/foo_log.log 2>&1

is it possible to use & to let all of them to run at the same time? if it is possible, can I run the following command to output the log only after all three of the above commands finished execution?

tail log/foo_log
+2  A: 

Easy enough, you can use wait to pause until all the processes have exited.

wait: wait [n]
Wait for the specified process and report its termination status. If N is not given, all currently active child processes are waited for, and the return code is zero. N may be a process ID or a job specification; if a job spec is given, all processes in the job's pipeline are waited for.

Voilà!

find ./incoming/kontraktor/ -type f -name '*.html' | sort | awk 'NR % 3 == 1' | ./bin/foo.py -m 3 -b 1 | next_command >> log/foo_log.log 2>&1 &
find ./incoming/kontraktor/ -type f -name '*.html' | sort | awk 'NR % 3 == 2' | ./bin/foo.py -m 3 -b 2 | next_command >> log/foo_log.log 2>&1 &
find ./incoming/kontraktor/ -type f -name '*.html' | sort | awk 'NR % 3 == 0' | ./bin/foo.py -m 3 -b 3 | next_command  >> log/foo_log.log 2>&1 &
wait
tail log/foo_log
John Kugelman
A: 

i saw this discussion while searching for solution, so i thought I would try answering my own question XD

(find ./incoming/kontraktor/ -type f -name '*.html' | sort | awk 'NR % 3 == 1' | ./bin/foo.py -m 3 -b 1 | next_command >> log/foo_log.log 2>&1) &
(find ./incoming/kontraktor/ -type f -name '*.html' | sort | awk 'NR % 3 == 2' | ./bin/foo.py -m 3 -b 2 | next_command >> log/foo_log.log 2>&1) &
(find ./incoming/kontraktor/ -type f -name '*.html' | sort | awk 'NR % 3 == 0' | ./bin/foo.py -m 3 -b 3 | next_command  >> log/foo_log.log 2>&1) &
wait
Jeffrey04