Visit Our UNIX and Linux User Community

To take script execution time

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To take script execution time
# 1  
Old 05-03-2017
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 i need to write inside he 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 
End time - 03:30:32
Total time taken taken - 01 hour

I need to display in hours and mins, like 01 hour 30 mins etc

Code i have tried but this is in seconds i need to display like above
start_time=$(date +%s)

finish_time=$(date +%s)
echo "Total time taken taken: $((finish_time - start_time)) secs."

# 2  
Old 05-03-2017
Does your system - the details of which you fail to mention - provide the time command?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-03-2017
It's just a simple matter of doing some arithmetic to the seconds. You've already got the data you'll need to work with.

Does this help? I just put a "sleep 12" in there so some time would pass between the start_time and finish_time.

start_time=$(date +%s)
sleep 12
finish_time=$(date +%s)

elapsed_time=$((finish_time  - start_time))

((sec=elapsed_time%60, elapsed_time/=60, min=elapsed_time%60, hrs=elapsed_time/60))
timestamp=$(printf "Total time taken - %d hours, %d minutes, and %d seconds." $hrs $min $sec)
echo $timestamp

It outputs like this:
Total time taken - 0 hours, 0 minutes, and 12 seconds.

Last edited by rbatte1; 05-03-2017 at 01:23 PM..
# 4  
Old 05-03-2017
Thanks for your input rothbat, is there a way to achieve without using arithmetic functions or any other way to achieve just wanted to know in simpler way.

@Rudic Sorry i dint get your question , can you let me know whether its achievable with time command i mean to calculate the time difference in hours and mins

Last edited by Master_Mind; 05-03-2017 at 01:20 PM.. Reason: Query
# 5  
Old 05-03-2017
Originally Posted by Master_Mind
Thanks for your input rothbat, is there a way to achieve without using arithmetic functions or any other way to achieve just wanted to know in simpler way.

@Rudic Sorry i dint get your question , can you let me know whether its achievable with time command i mean to calculate the time difference in hours and mins
I'm not sure how you're going to get the elapsed time without arithmetic if you are starting with what you have... starting and ending times...

Rudic's suggesting of the time command is definitely another option as it's a command designed to report back how long some other command/script took to execute, but you may have to be creative about when/where you use it since it reports back how long a single step (which can be another script) took to execute.

For example the "sleep 4" command takes 4 seconds to execute, but if you issue "time sleep 4" it reports back more detailed info on the command.
$ time sleep 4

real    0m4.035s
user    0m0.000s
sys     0m0.008s

I'm not sure if it's useful for you to have the elapsed time displayed that way. You can set the format it uses by passing in a format string but frankly, if you ask me... dealing with the simple arithmetic is the most straight forward way to handle it.

If you have 2 steps inside a script that both take time, you can't call them separate with time and have it reflect one time, it'll simply reflect how long each step took individually.
# 6  
Old 05-03-2017
( time ) 2>&1 | awk '{print}; NF==2 && $1 ~ /^real$/ {print "Total time taken - " $2; exit}'

Last edited by rdrtx1; 05-03-2017 at 02:47 PM..
# 7  
Old 05-03-2017
I'm not sure if his requirement is rigid, but he did say he needed to reflect the elapsed time "inside" his script. If he misspoke, then time is what I'd use and your solution looks nice.

Previous Thread | Next Thread
Test Your Knowledge in Computers #895
Difficulty: Medium
The GNU Core Utilities or coreutils is a package of GNU software containing reimplementations for many of the basic tools, such as cat, ls, and rm, which are used on Unix-like operating systems.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Optimizing script to reduce execution time 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

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

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

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

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

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

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