Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to display the completion time of a script.

What I currently do is -

#!/bin/bash
date  ## echo the date at start
# the script contents
date  ## echo the date at end

This just show's the time of start and end of the script. Would it be possible to display a fine grained output like processor time/ io time , etc?

share|improve this question

6 Answers 6

up vote 33 down vote accepted

just use time when you call the script.

time yourscript.sh
share|improve this answer
#!/bin/bash
start=$(date +%s.%N)

# HERE BE CODE

end=$(date +%s.%N)    
runtime=$(python -c "print(${end} - ${start})")

echo "Runtime was $runtime"

Yes, this calls Python, but if you can live with that then this is quite a nice, terse solution.

share|improve this answer

Here's a variation of Alex's answer. I only care about minutes and seconds, but I also wanted it formatted differently. So I did this:

start=$(date +%s)
end=$(date +%s)
runtime=$(python -c "print '%u:%02u' % ((${end} - ${start})/60, (${end} - ${start})%60)")
share|improve this answer

I'm a bit late to the bandwagon, but wanted to post my solution (for sub-second precision) in case others happen to stumble upon this thread through searching. The output is in format of days, hours, minutes, and finally seconds:

res1=$(date +%s.%N)

# do stuff in here

res2=$(date +%s.%N)
dt=$(echo "$res2 - $res1" | bc)
dd=$(echo "$dt/86400" | bc)
dt2=$(echo "$dt-86400*$dd" | bc)
dh=$(echo "$dt2/3600" | bc)
dt3=$(echo "$dt2-3600*$dh" | bc)
dm=$(echo "$dt3/60" | bc)
ds=$(echo "$dt3-60*$dm" | bc)

printf "Total runtime: %d:%02d:%02d:%02.4f\n" $dd $dh $dm $ds

Hope someone out there finds this useful!

share|improve this answer

Just call time or times without arguments upon exiting your script.

times is the POSIX way, but time is supported by ksh, bash and zsh but with different information provided.

Also note that all of bash, ksh and zsh have a $SECONDS special variable that counts the number of seconds since the shell was started. In both zsh and ksh93, that variable can also be made floating point (with typeset -F SECONDS) to get more precision. This is only wall clock time, not CPU time.

share|improve this answer
    
That $SECONDS variable is very useful, thanks! –  andybuckley Jun 18 at 12:00

If time isn't an option,

start=`date +%s`
stuff
end=`date +%s`

runtime=$((end-start))
share|improve this answer
2  
Note that this only works if you don't need sub-second precision. For some uses that might be acceptable, for others not. For slightly better precision (you're still invoking date twice, for example, so you might at best get millisecond precision in practice, and probably less), try using date +%s.%N. (%N is nanoseconds since the whole second.) –  Michael Kjörling Oct 19 '12 at 19:17
    
Good point. I thought of that just after leaving the keyboard but didn't come back. ^^ Also remember, OP, that "date" will itself add a few milliseconds to the run time. –  Rob Bos Oct 20 '12 at 15:54
    
This is nice, but with the improvement from @MichaelKjörling I get the following error:bash: 1390472071.282341976: syntax error: invalid arithmetic operator (error token is ".282341976"), so maybe this needs more clarification. –  Chris H Jan 23 at 10:15
    
@ChrisH Oh. Good pointing it out; bash arithmetic expansion is integer-only. I see two obvious options; either ditch the period in the date format string (and treat the resultant value as nanoseconds since epoch), so use date +%s%N, or use something more capable like bc to calculate the actual runtime from the two values like jwchew suggests. Still, I feel this approach is a suboptimal way of doing it; time is considerably better if available, for reasons outlined above. –  Michael Kjörling Jan 23 at 13:29
    
@MichaelKjörling, that explains it, I'm a bash novice so hadn't realised why. The nice ting about this approach is that it's easy to drop it in to an existing script and multiple (overlapping if necessary) timers can be used. I was using it to time a LaTeX compile process at around 20 seconds so 1 second precision was enough for me. Thanks for coming back to such an old thread so quickly! –  Chris H Jan 23 at 14:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.