How to know the execution time in linux How to know the execution time in linux shell shell

How to know the execution time in linux


Use wait:

first=`date +%s`./a.sh &./b.sh &./c.sh &wait...

Quoting help wait:

wait: wait [id]

Wait for job completion and return exit status.Waits for the process identified by ID, which may be a process ID or ajob specification, and reports its termination status.  If ID is notgiven, waits for all currently active child processes, and the returnstatus is zero.  If ID is a a job specification, waits for all processesin the job's pipeline.Exit Status:Returns the status of ID; fails if ID is invalid or an invalid option isgiven.


linux comes with the time command that is especiall designed for this exact usage. Just call time yourscript.

As your launcher script starts processes in the background, time alone isn't sufficient. As stated by @devnull, there is the wait 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:

time `./launcher.sh;wait `

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 &

test2.sh

sleep 10

Result:

$ time `./test1.sh;wait`

real 0m10.004s
user 0m0.001s
sys 0m0.001s


Just before exit your script write

echo $SECONDS

or to run your script in a terminal windows, type

$> time your_script