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

Does your system - the details of which you fail to mention - provide the time command?

1 Like

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.

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.

( time script.sh ) 2>&1 | awk '{print}; NF==2 && $1 ~ /^real$/ {print "Total time taken - " $2; exit}'

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.

man time :

/usr/bin/time -f%E  sleep 400
6:40.00