Solved [Solved] Count running time of a script

Hello,

How do you guys count running time of a script. I could use

Code:
time script

but I want to implement that time counting within the script so I could run ./script.sh and at the end to echo running time.
 
Code:
#!/bin/sh
starttime=$( /bin/date +%s )

[ ... stuff from original script ..]

stoptime=$( /bin/date +%s )
runtime=$( /bin/expr ${stoptime} - ${starttime} )
echo "Runtime: ${runtime} seconds."
 
To make it 'portable', create a script called runtime, put this in it:

Code:
#!/bin/sh
starttime=$( /bin/date +%s )

$*

stoptime=$( /bin/date +%s )
runtime=$( /bin/expr ${stoptime} - ${starttime} )
echo "Runtime: ${runtime} seconds."

and run it if you want to know the runtime of anything, e.g.

[cmd=]runtime ps ax[/cmd] or
[cmd=]runtime make buildworld[/cmd].

It's basically the 'time' command, but with a simple 'real time seconds' value.
 
Back
Top