Tell me more ×
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

3 Answers

up vote 22 down vote accepted

just use time when you call the script.

time yourscript.sh
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

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

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.