script execution time calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script execution time calculation
# 1  
Old 02-24-2006
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
# 2  
Old 02-24-2006
I use this in our build script.

Code:
        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 ))

Last edited by vino; 02-24-2006 at 03:58 AM..
# 3  
Old 02-24-2006
date %s is a GNU extension so may not be available. You could use the ksh built-in variable $SECONDS instead.
# 4  
Old 02-24-2006
Quote:
Originally Posted by Ygor
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. Smilie

Hmm... I should modify the script !
# 5  
Old 02-24-2006
hi

Quote:
Originally Posted by vino
Thanks Ygor for pointing that out. Smilie

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
# 6  
Old 02-24-2006
Quote:
Originally Posted by vishallbansal
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

Code:
strings `which date` | grep '%s'

You should heed Ygor's suggestion.

Last edited by vino; 02-24-2006 at 05:29 AM..
# 7  
Old 02-24-2006
$ 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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To take script execution time

Hello Guys, I would like to know is there a way to take the script execution time For e.g i am having a script.sh i need to write inside he script.sh like Start time : 10-Mar-2016 02:30:35 all code over here ... End time : 10-Mar-2016 03:30:32 Script start time - 02:30:35 ... (7 Replies)
Discussion started by: Master_Mind
7 Replies

2. Shell Programming and Scripting

Optimizing script to reduce execution time

AFILENAME=glow.sh FILENAME="/${AFILENAME}" WIDTHA=$(echo ${FILENAME} | wc -c) NTIME=0 RESULTS=$(for eachletter in $(echo ${FILENAME} | fold -w 1) do WIDTHTIMES=$(awk "BEGIN{printf... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

How to do simple date (time) calculation in shell script?

Hi, I'm looking for a way to do a simple math calc during a shell script as a means of logging how long a particular task takes. For example... STARTTIME=whenever this script starts ./path/to/command.sh >>logfile.log TOTALTIME=<time at this stage of the script after above command... (7 Replies)
Discussion started by: nbsparks
7 Replies

4. Shell Programming and Scripting

Find execution time of script

i am using bash START=$(date +%s) END=$(date +%s) DIFF=$(echo "$END - $START" ) this code is not working (14 Replies)
Discussion started by: rafa_fed2
14 Replies

5. Shell Programming and Scripting

time calculation in ksh script

I"m trying to calculate the duration of of backup within a ksh shell script but I get an error. #!/bin/ksh STTIM=`date '+%T'` EDTIM=`date '+%T'` .... .... echo "DURATION OF BACKUP: $((EDTIM - STTIM))" (5 Replies)
Discussion started by: Bperl1967
5 Replies

6. Shell Programming and Scripting

execution time / runtime -- bash script please help!

Hello, I'm running a bash script and I'd like to get more accurate a runtime information then now. So far I've been using this method: STARTM=`date -u "+%s"` ......... *script function.... ......... STOPM=`date -u "+%s"` RUNTIMEM=`expr $STOPM - $STARTM` if (($RUNTIMEM>59)); then... (6 Replies)
Discussion started by: TehOne
6 Replies

7. UNIX for Dummies Questions & Answers

script start and end of execution of time

Hi All, Can we get to know the start time and end time of execution of a script? (This script doesn't write any logs.) I mean, is there any built in process logs to track these records? (2 Replies)
Discussion started by: siba.s.nayak
2 Replies

8. Shell Programming and Scripting

get execution time of a script

Hi, I have a simple question. How can I get the execution time of a script and maybe put it in a variable? Another question. How can I get only time and not date and put it in a variable? I tried something with "date" command but with no success... If someone could help me... (8 Replies)
Discussion started by: Moumou
8 Replies

9. Shell Programming and Scripting

time of execution of script

i want to test whether a script has been executed in last 15 days or not....please help how can i do this...is there any copmmand there to know timings of last execution of any script (8 Replies)
Discussion started by: arghya_owen
8 Replies

10. UNIX for Advanced & Expert Users

time calculation

Hi, I have start time as a string like 06:04:01 and end time like 06:05:01 i need do a simple math to get the duration. What is the best way to do this in Korn Shell scripting? Thanks (2 Replies)
Discussion started by: liux99
2 Replies
Login or Register to Ask a Question

Featured Tech Videos