Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

measuring runtime of bash script with & parameter in the body

How can I measure the runtime of my script which is like this :

#! /bin/bash
start1=`date +%s`
./a &
./a &
./a &
./a &
./a &

end=`date +%s`
runtime=$((end-start1))
echo "$runtime"

the measured time is 0 because all the command run in background and at the same time what should I do?

like image 857
Farzad Salimi Jazi Avatar asked Aug 20 '13 16:08

Farzad Salimi Jazi


1 Answers

Use the wait command to wait for the processes to finish, before measuring the time

#! /bin/bash
start1=`date +%s`
./a &
./a &
./a &
./a &
./a &

wait #  <----------------

end=`date +%s`
runtime=$((end-start1))
echo "$runtime"

You can also use the time command for better and more accurate results:

time {
    ./a &
    ./a &
    ./a &
    ./a &
    ./a &

    wait #  <----------------
}
like image 187
user000001 Avatar answered Sep 21 '22 23:09

user000001