script execution time calculation

I am writting a script in the ksh shell and am trying to find a way to report the total execution time of the script without requiring the user to specify the time function when executing the script.
Does anyone have any examples they have used. I have been setting up two date variables (one at the beginning $START_TIME and one at the end $END_TIME) and then do some calulation of the difference and display is to the user in minutes. No matter what I do it keeps giving me syntax errors.
any help

I use this in our build script.

        start_time=$(date +%s)
.
.
.
        finish_time=$(date +%s)
        echo "Time duration: $((finish_time - start_time)) secs."

Divide the above by 60 to get minutes.

Something like

min=$(( $((finish_time - start_time)) /60 ))

date %s is a GNU extension so may not be available. You could use the ksh built-in variable $SECONDS instead.

Thanks Ygor for pointing that out. :slight_smile:

Hmm... I should modify the script !

hye dude the syntax for setting start_date is not working...
i am workin in bash...
i too tried out printing the time taken by a process

What error are you encountering ?

Possibly you dont have GNU date. Post the results of

strings `which date` | grep '%s'

You should heed Ygor's suggestion.

$ strings `which date` | grep '%s'
$
Returns nothing
$ which date
/usr/bin/date
$

Here is the code I have been testing with:
#!/bin/ksh
start=$(date +%s)
time sleep 20
end=$(date +%s)
echo "Time: $((end - start)) secs."

Results: syntax error

changed to:
#!/bin/ksh
start=$SECONDS
time sleep 20
end=$SECONDS
echo "Time: $((end - start)) secs."

Results: This works!

My question now is, will it still work after hours of execution. How ofter does this value get reset?

Here is the original code I was trying to get to work:
#!/bin/ksh

BDAY=$(date +%e)
((BTIME=($(date +%H)*3600)+($(date +%M)*60)+$(date +%S)))

time sleep 20

EDAY=$(date +%e)
# assumes run time not over 48 hours.
[[ $BDAY == $EDAY ]] && DAY=0 || DAY=86400
((ETIME=$DAY+($(date +%H)*3600)+($(date +%M)*60)+$(date +%S)))

((TOTAL=$ETIME - $BTIME))
print "Run Time: \c"
((HOURS=TOTAL / 3600))
((TOTAL=TOTAL - (HOURS*3600)))
print "$HOURS hours, $((TOTAL / 60)) minutes, $((TOTAL % 60)) seconds."

The above code has given me a variety of error messages that I have fixed and the latest is:

real 0m20.02s
user 0m0.00s
sys 0m0.00s
./fun4[13]: syntax error at line 16 : `==' unexpected

Maybe you can see what is wrong - Thanks

the 'time sleep 20' line doesn't make sense to use. it's only timing the sleep command which you know will execute for only 20 seconds. er... apparently 20.02 seconds. :smiley:

maybe you can wrap a script around it which uses the 'time' command against the script you wanna run. just a thought. *shrugs*

The time sleep 20 is used for nothing more than testing. the real script is preforming some software and database upgrades. The only reason why i used the time command is so it would actually display the 20 sec to the screen. just a habbit of mine