Suppose that I have three script(a.sh b.sh c.sh) to be launched in a script called launcher.sh:
first=`date +%s`
./a.sh &
./b.sh &
./c.sh &
final=`date +%s`
executionTime=`echo "scale=3;($final - $first)/3600" | bc`
echo $executionTime
I know above script won't work because launcher.sh will finish immediately after launching a.sh, b.sh and c.sh. Launching a.sh, b.sh and c.sh without "&" is wasting time because they don't have to wait for each other.
Suppose that the execution time for them are 5 seconds, 10 seconds and 7 second respectively, how can I get 10 seconds as the overall execution time?
Use
wait
:Quoting
help wait
:Just before exit your script write
or to run your script in a terminal windows, type
linux comes with the
time
command that is especiall designed for this exact usage. Just calltime yourscript
.As your launcher script starts processes in the background,
time
alone isn't sufficient. As stated by @devnull, there is thewait
command that blocks a call until all subprocesses have terminated.There is an easy way to combine wait and time, without modifying your launcher script and without the need to manually stop the time:
should do the trick.
(tried this with a minimal example. as long as you execute the launcher + wait inside a subshell (using ``), this will work (but you loose otuput from the launcerh script if it does output something))
Minimal example:
test1.sh
test2.sh
Result: