Solved [Solved] Count running time of a script

overmind

Well-Known Member

Reaction score: 32
Messages: 332

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.
 

DutchDaemon

Administrator
Staff member
Administrator
Moderator
Developer

Reaction score: 2,780
Messages: 11,279

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

[ ... stuff from original script ..]

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

DutchDaemon

Administrator
Staff member
Administrator
Moderator
Developer

Reaction score: 2,780
Messages: 11,279

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.
 
Top