Reputation: 6338
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
Upvotes: 0
Views: 1000
Reputation: 6338
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
Upvotes: 0
Reputation: 361565
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
Upvotes: 2