Total Time

Hello,
I have one script which takes some time to complete.

I Need the total exact time taken by this script.

How can i modify this script.

Regards,
Sam.

time script.sh

hmmm
where should i write the above line...
For example my script name is my_script.ksh...

should i write time my_script.sh inside the same script...

no, you want to invoke the script with time command before it.
Like

time my_script.ksh

Look at

man time 

---------- Post updated at 11:05 PM ---------- Previous update was at 11:03 PM ----------

$ time tar cvzf ms.tar MS1.wav 
MS1.wav

real    0m5.140s
user    0m5.020s
sys    0m0.140s
time COMMAND

will print time it takes to run COMMAND on stderr.

Cant i directly write in my_script.ksh at the end of the script and as soon as the script is ove it should display on the screen saying that the scrip is sucessfully excuted and the total time taken by the script is 01:00 hrs..some thing like that...

You can do something like :

#!/bin/ksh
t0=`date +%s.%N`

#do the hard work here

t1=`date +%s.%N`

dt=$(echo "$t1-$t2" | bc -l)
echo "It took me $dt seconds. Ufff.." 
exit 0

Still not satisfied...Waited, tried doing google and forgotten...
I have one script called abc.ksh, which i execute...

I just need the total time it took to execute the script and display on the screen stating the total time...

create one file and give execute permission to the file

write the below line the file

time your-original-script-name

execute the file, you will get the time for execution

Ok, here you go....
Once again
i needed the total time taken by my script called abc.ksh...like 20 mins or 30 mins or anything to be displayed on scrren as soon as abc.ksh is finished.

What i did as per your suggestion, i created a new script something called fff.ksh and inserted a line in it as below:

time abc.ksh

gave a proper permission..

Next what i did is, inside my original script at the end of abc.ksh, i wrote
/mypath/fff.ksh
below is the output when abc.ksh finished...It didnt gave me any time...

real    0m0.001s
user    0m0.000s
sys     0m0.000s

I gave you two solutions in the previous posts. What exactly is not working there?

Why?

time(1) gives you elapsed time of command specified as its argument. so it gave you the time it took to execute fff.ksh. But you wanted the time it took to run abc.ksh. So do

time abc.ksh 

instead of

./abc.ksh

, when you are invoking the script.

OK here you go....
My name of the script is abc.ksh which is approximately taking 8 to 9 mins to execute but iam not sure how exact it is ...so i needed help so posted here...
my abc.ksh script is as below...

#! /bin/ksh
pun=/usr/mi/tics/Par
/$pun/ihy_ed.ksh
ret=$?
if [ $ret -ne 0 ]
then
        echo
        echo " =====> ERROR OCCURRED WHILE RUNNING ihy_ed.ksh <===== "
        echo " =====> ERROR CODE RETURNED BY ihy_ed.ksh=$ret <===== "
exit $ret
else
        echo " ====> THE SCRIPT COMPLETED WITHOUT ANY ERRORS ====> "
        echo
fi

The above code for the script name abc.ksh works absolutely fine and its taking 8 to 9 mins but dont know the exact time...
I execute it the the below way...

./abc.ksh

now @mirni-- as you said just put the time before the abc.ksh and i did that after the abc.ksh is completely executed.
And below is the out put on the screen after i wrote
-----time abc.ksh

-bash: abc.ksh: command not found
real    0m0.001s
user    0m0.000s
sys     0m0.000s

its not displaying above like 8 or 9 mins as such..

---------- Post updated at 12:27 PM ---------- Previous update was at 12:24 PM ----------

What exactly should i do in my script (abc.ksh) so that it will show me in the end saying that the script took 8 or 9 or any mins or hours to execute..

you are executing wrong , i mean without the relative path.

time ./abc.ksh

This will work

ok thanks @Kumaran..i will surely try it....
one more thing where should i incorporate the above line ..
do u mean i should put it in the end of my script (abc.ksh)...so that as soon as abc.ksh is finished it will give me the total time it completed...

No. You have to execute your script using that line.

usually you execute your command with

prompt> ./abc.ksh

But now

prompt> time ./abc.ksh

fantastic...that was so simple...i did it and got the out put at the end...
a slight confusion
i got 3 lines
real 7m17.844s
user 0m5.168s
sys 0m1.736s

what exactly are these and which one is my time for abc.ksh script....

You may refer the man page of time.

real - starts counting from the moment you start the command to the command's termination, includes all scheduling delays, this would be your elapsed time.
user - how much time it has executed in used mode
sys - how much time in kernel mode

       o  The elapsed (real) time between invocation  of  utility
          and its termination.

       o  The User  CPU  time,  equivalent  to  the  sum  of  the
          tms_utime   and   tms_cutime  fields  returned  by  the
          times(2) function for the process in which  utility  is
          executed.

       o  The System CPU time,  equivalent  to  the  sum  of  the
          tms_stime and tms_cstime fields returned by the times()
          function for the process in which utility is executed.


ok..thanks.Kumaran
One last thing...
if i need to display/print the time using echo or some other command after the completion of abc.ksh, how can it be done..

rename your abc.ksh to abc_wrapped.ksh. and create new abc.ksh as below

#!/bin/ksh
echo `time .\abc_wrapped.ksh`

Make sure you put both the script in the same directory or change the path

hmmm.
Cant i create a new abc_wrapped.ksh and put your code in that...
Why Renaming the name of my script.

you can do either way. I just suggested my way. All you need a wrapper script which will have small code to print the time and will call your original script.