Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine How Long a Script Takes to Run 1

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
0
0
US
Using the standard Solaris 8 shell I need to compare an application's start and end time, i.e. I need to know how long the script took to run.

Does anyone have a script snippet on how to do this?



Thanks,

Michael42
 
man time

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
use timex

see man timex

timex ls -al

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
You could also use the shell built-in variable $SECONDS

#!/bin/sh
START=$SECONDS
:
#script commands go here
:
END=$SECONDS
echo elapsed time is $((END-START)) seconds

 
Thanks all. You have given me a few good options. :)

Thanks,

Michael42
 
Ygor:
Your solution looks so tidy, but I get syntax errors when I try it, plus what if the date changes?

Try this on for size...a bit more complex, but works so well!

#!/bin/csh -f

# display the start date and time
set start_time = `date`
echo 'Starting on' $start_time[1-3] 'at' $start_time[4-6]
@ h1 = `date '+%H'`
@ m1 = `date '+%M'`
@ s1 = `date '+%S'`
:
#script commands go here
:
# display the stop time
set stop_time = `date`
echo 'completed on' $stop_time[1-3] 'at' $stop_time[4-6]

@ h2 = `date '+%H'`
@ m2 = `date '+%M'`
@ s2 = `date '+%S'`

# total time calculations
@ start_time_2 = $h1 * 3600 + $m1 * 60 + $s1
#echo start time $start_time_2
@ stop_time_2 = $h2 * 3600 + $m2 * 60 + $s2
#echo stop_time $stop_time_2

if ( $h2 < $h1 ) then
@ stop_time_2 = $stop_time_2 + 86400
#echo corrected stop time $stop_time_2
endif

@ total_time_2 = $stop_time_2 - $start_time_2
#echo total time $total_time_2
@ hours = $total_time_2 / 3600
@ total_time_2 = $total_time_2 % 3600
@ minutes = $total_time_2 / 60
@ seconds = $total_time_2 % 6

echo Script required : $hours hours $minutes minutes $seconds secs

================
This snippet is courtesy of Duff A. Robbins. He wrote it in 1999 and we are still using it Today!
 
Maybe use tcl:

proc timeit {args} {
set start [clock seconds]
eval exec $args
set end [clock seconds]
puts &quot;Elapsed Time: [expr $end - $start]&quot;
}


 
Can someone give me the sh version of Duane61's program?

Many thanks!
 
set -- `date`
echo "Starting on $1 $2 $3 at $4 $5 $6"
h1=`date '+%H'`
m1=`date '+%M'`
s1=`date '+%S'`
:
#script commands go here
:
# display the stop time
set -- `date`
echo "Completed on $1 $2 $3 at $4 $5 $6"
h2=`date '+%H'`
m2=`date '+%M'`
s2=`date '+%S'`
# total time calculations
start_time_2=`expr \( $h1 \* 3600 \) + \( $m1 \* 60 \) + $s1`
#echo start time $start_time_2
stop_time_2=`expr \( $h2 \* 3600 \) + \( $m2 \* 60 \) + $s2`
#echo stop_time $stop_time_2
if [ $h2 -lt $h1 ]; then
stop_time_2=`expr $stop_time_2 + 86400`
#echo corrected stop time $stop_time_2
fi
total_time_2=`expr $stop_time_2 - $start_time_2`
#echo total time $total_time_2
hours=`expr $total_time_2 / 3600`
total_time_2=`expr $total_time_2 % 3600`
minutes=`expr $total_time_2 / 60`
seconds=`$total_time_2 % 60`
echo "Script required : $hours hours $minutes minutes $seconds secs"

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thanks PHV it works great, just a small mistake the line
seconds=`$total_time_2 % 60`
should be seconds=`expr $total_time_2 % 60`

Also what does set -- `date` do exactly?

 
[tt]set arg1 arg2 ... argn[/tt] assign arg1, arg2, ... to $1, $2, ... in order.
The [tt]--[/tt] flag prevents the set command to change the shell's flags even if arg1 is, say, -x.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
hehe I gotta contribute my 1 and 1/2 cents worth...

Code:
# Start of the script
STARTTIME=$SECONDS

sleep 999
echo "woohoo!!!"

ENDTIME=$SECONDS
DURATION=$(( ENDTIME - STARTTIME ))
# 3600 seconds in an hour
if [ $DURATION -gt 3600 ]; then
    DURATION=`echo "scale=2; $DURATION / 3600" | bc`
    UNIT="hours";
elif [ $DURATION -gt 60 ]; then
    DURATION=`echo "scale=2; $DURATION / 60" | bc`
    UNIT="minutes"
else
    UNIT="seconds"
fi

I actually don't use [tt]$SECONDS[/tt]. I use [tt]STARTTIME=`perl -e 'print time'`[/tt]

--
-- GhodMode
 

adapted from a Perl function...
Code:
CalculateDuration()
{
    CD_START="$1"
    CD_END="$2"
    CD_TOTAL=$(( CD_END - CD_START ))

    HOURS=0
    MINUTES=0
    REMAINING_SECONDS=0

    # First, get the number of seconds left after dividing
    # by the number of seconds in an hour (60 * 60)
    HOURS_REMAINDER=$(( CD_TOTAL % (60*60) ))

    # Then, subtract that from the total, and you have the
    # number of hours, in seconds
    HOURS_SECONDS=$(( CD_TOTAL - HOURS_REMAINDER ))

    # Then, divide by the number of seconds in an hour
    # (60 * 60) to get just the hours
    HOURS=$(( HOURS_SECONDS / (60*60) ))

    # Get the number of seconds remaining from calculating
    # the hours, and divide that by the number of seconds in
    # a minute (60)
    MINUTES_REMAINDER=$(( HOURS_REMAINDER % 60 ))

    # Then, subtract that from the total number of seconds
    # remaining from calculating the hours, and you have the
    # number of minutes, in seconds
    MINUTES_SECONDS=$(( HOURS_REMAINDER - MINUTES_REMAINDER ))

    # Then, divide by the number of seconds in a minute (60)
    # to get just the minutes
    MINUTES=$(( MINUTES_SECONDS / 60 ))

    # Whatever remains is the seconds
    REMAINING_SECONDS=$MINUTES_REMAINDER

    [ $HOURS             -lt 10 ] && HOURS="0$HOURS"
    [ $MINUTES           -lt 10 ] && MINUTES="0$MINUTES"
    [ $REMAINING_SECONDS -lt 10 ] && REMAINING_SECONDS="0$REMAINING_SECONDS"

    DURATION="${HOURS}:${MINUTES}:${REMAINING_SECONDS}"
} # End CalculateDuration function


--
-- GhodMode
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top