Reputation: 4521
I have a bash script which runs 4 different process in the background and you can see the code below:
declare -a arr=("seed_automation_data_1" "seed_automation_data_2" "seed_automation_data_3" "seed_automation_data_4")
command="bundle exec rake db:seed:"
for i in "${arr[@]}"
do
$command$i &
done
This bash script is actually running a rake task
in rails framework
.
$command$i &
This particular line is starting four different process in the background:-
bundle exec rake db:seed:seed_automation_data_1
bundle exec rake db:seed:seed_automation_data_2
bundle exec rake db:seed:seed_automation_data_3
bundle exec rake db:seed:seed_automation_data_4
Since there are four different process running in the background I am not able to know when the bash script
is FINISHED
or calculate the execution-time
of it.
Is there a way I can print some statements which will show that the script is finished running?
Upvotes: 3
Views: 2071
Reputation: 33685
Looks like a job for GNU Parallel:
declare -a arr=("seed_automation_data_1" "seed_automation_data_2" "seed_automation_data_3" "seed_automation_data_4")
parallel --joblog - bundle exec rake db:seed:{} ::: "${arr[@]}"
GNU Parallel is a general parallelizer and makes is easy to run jobs in parallel on the same machine or on multiple machines you have ssh access to. It can often replace a for
loop.
If you have 32 different jobs you want to run on 4 CPUs, a straight forward way to parallelize is to run 8 jobs on each CPU:
GNU Parallel instead spawns a new process when one finishes - keeping the CPUs active and thus saving time:
Installation
For security reasons it is recommended you use your package manager to install. But if you cannot do that then you can use this 10 seconds installation.
The 10 seconds installation will try to do a full installation; if that fails, a personal installation; if that fails, a minimal installation.
$ (wget -O - pi.dk/3 || lynx -source pi.dk/3 || curl pi.dk/3/ || \
fetch -o - http://pi.dk/3 ) > install.sh
$ sha1sum install.sh | grep 883c667e01eed62f975ad28b6d50e22a
12345678 883c667e 01eed62f 975ad28b 6d50e22a
$ md5sum install.sh | grep cc21b4c943fd03e93ae1ae49e28573c0
cc21b4c9 43fd03e9 3ae1ae49 e28573c0
$ sha512sum install.sh | grep da012ec113b49a54e705f86d51e784ebced224fdf
79945d9d 250b42a4 2067bb00 99da012e c113b49a 54e705f8 6d51e784 ebced224
fdff3f52 ca588d64 e75f6033 61bd543f d631f592 2f87ceb2 ab034149 6df84a35
$ bash install.sh
For other installation options see http://git.savannah.gnu.org/cgit/parallel.git/tree/README
Learn more
See more examples: http://www.gnu.org/software/parallel/man.html
Watch the intro videos: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
Walk through the tutorial: http://www.gnu.org/software/parallel/parallel_tutorial.html
Sign up for the email list to get support: https://lists.gnu.org/mailman/listinfo/parallel
Upvotes: 4
Reputation: 15934
Have a look at the wait
function in bash
. It simply waits for all child processes to finish. Then you can easily calculate the time elapsed e.g. using the SECONDS
internal variable (explained here):
SECONDS=0
declare -a arr=("seed_automation_data_1" "seed_automation_data_2" "seed_automation_data_3" "seed_automation_data_4")
command="bundle exec rake db:seed:"
for i in "${arr[@]}"
do
$command$i &
done
wait
echo $SECONDS
Upvotes: 6