Visit Our UNIX and Linux User Community


get execution time of a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get execution time of a script
# 1  
Old 07-29-2009
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...
# 2  
Old 07-29-2009
Quote:
Originally Posted by Moumou
I have a simple question. How can I get the execution time of a script and maybe put it in a variable?
Code:
$ script_times=$(/usr/bin/time -p my_script 3>&2 2>&1 1>&3 3>&-)
$ echo "$script_times

real   0.01
user   0.00
sys    0.01
$

Quote:
Originally Posted by Moumou
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...
Code:
$ current_time=$(date +%T)
$ echo $current_time
11:26:20
$ current_time=$(date +'%H.%M.%S')
$ echo $current_time              
11.27.50
$

Jean-Pierre.
# 3  
Old 07-29-2009
ok, thx a lot for your answer.

Another question : what is the difference between real time, user time and system time?

And I just realized that if forgot a part of my question previously so I ask it now :

In fact i run a script which is calling other scripts. And what i'd like to do is to print at the end of each children script something like
Quote:
"elapsed time : xxhyymzzs"
Thx for your answers

Last edited by Moumou; 07-29-2009 at 08:38 AM..
# 4  
Old 07-29-2009
You can do something like that :
Code:
elapsed()
{
   (( seconds  = SECONDS ))
   "$@"
   (( seconds = SECONDS - seconds ))
   (( etime_seconds = seconds % 60 ))
   (( etime_minuts  = ( seconds - etime_seconds ) / 60 % 60 ))
   (( etime_hours   = seconds / 3600 ))
   (( verif = etime_seconds + (etime_minuts * 60) + (etime_hours * 3600) ))
   echo "elapsed time: ${etime_hours}h${etime_minuts}m${etime_seconds}s"
 }

elapsed sleep 67

Output:
Code:
elapsed time: 0h1m7s

Jean-Pierre.
# 5  
Old 07-29-2009
Thx a lot Jean Pierre,
I tested it and it works.... in bash shell. And i need it for a csh script and here it doesn't work (snif..)
When I just copying the script it send me this error :
Quote:
Badly placed ()'s
and when I remove the first (), it worst... :
Quote:
elapsed: Command not found.
{: Command not found.
seconds: Command not found.
Illegal variable name
I'll try to find out something by myself, but if you have any suggestion, i'll appreciate it!

And sorry not to tell what kind of shell i was writting at the beginning.... SmilieSmilie

Regards

Last edited by Moumou; 07-29-2009 at 12:44 PM.. Reason: forget to say sorry...
# 6  
Old 07-30-2009
Execution time of a script

Hi,

I don't know if I can ask, but I found this function very interesting..

PHP Code:
elapsed()
{
   (( 
seconds  SECONDS ))
   
"$@"
   
(( seconds SECONDS seconds ))
   (( 
etime_seconds seconds 60 ))
   (( 
etime_minuts  = ( seconds etime_seconds ) / 60 60 ))
   (( 
etime_hours   seconds 3600 ))
   (( 
verif etime_seconds + (etime_minuts 60) + (etime_hours 3600) ))
   echo 
"elapsed time: ${etime_hours}h${etime_minuts}m${etime_seconds}s"
 

At first time I didn't try to make it work, but I was wrong. In fact each time I am using this function on my script I get
PHP Code:
elapsed time0h0m0s 
Do you mind to tell me how to make it work correctly ? can you please help me ?

Thanks

Last edited by Aswex; 07-30-2009 at 07:28 AM.. Reason: The script is working correctly on KSH
# 7  
Old 07-30-2009
How are you using this function?
if you are doing something like this :
Code:
code code
code code
code code
elapsed

I understand why it doesn't works. You have to specify on which script you want to apply elapsed.
e.g :
Code:
elapsed yourscript.sh

Jean-Pierre used "sleep 67" as an example.

Regards

Previous Thread | Next Thread
Test Your Knowledge in Computers #641
Difficulty: Medium
NeXTSTEP had a Unix operating system based on the Minix kernel, plus source code from System V.
True or False?

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

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

4. UNIX for Dummies Questions & Answers

time taken for execution

how much time a particular command or shell script executed there is any command to know this thanks (5 Replies)
Discussion started by: tsurendra
5 Replies

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

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

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

8. Shell Programming and Scripting

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... (9 Replies)
Discussion started by: johnsonbryce
9 Replies

9. UNIX for Dummies Questions & Answers

last execution time

is there a command in Solaris 8 that will show a particular scripts last execution time? (1 Reply)
Discussion started by: cubs0729
1 Replies

10. Programming

execution time

hi , i ve coded a C program in that im using malloc dynamically , it is being called many times in the program The program is to simulate jobs in manufacturing system. the execution time is increasing drastically as the number of jobs are increased. could any body tel what may be the problem... (2 Replies)
Discussion started by: ramki_rk
2 Replies

Featured Tech Videos