0

Infact when i want to have an estimated time it took when a script is finished.

startdate=$date

stuffs

enddate=$date

I wished to get the difference between these two dates and normally the script finishes less than 1 week. So i'll need it in number of days & minutes.

2
  • 3
    Have you tried using the $SECONDS variable that counts seconds since the script started? – Kusalananda May 26 '20 at 13:49
  • Hi Kusalananda, It works with the solution you provided below. – Keshav Boodhun May 29 '20 at 4:57
3

From the bash manual:

SECONDS

Each time this parameter is referenced, the number of seconds since shell invocation is returned. If a value is assigned to SECONDS, the value returned upon subsequent references is the number of seconds since the assignment plus the value assigned. If SECONDS is unset, it loses its special properties, even if it is subsequently reset.

So whenever you want to time a piece of code with a timer that counts seconds, you may do

SECONDS=0

# code goes here

t=$SECONDS

To print days and minutes from the $t value:

printf 'Time taken: %d days, %d minutes\n' "$(( t/86400 ))" "$(( t/60 - 1440*(t/86400) ))"
2
0

You can use timestamps with date "+%s"

$ date "+%s"
1590502845

And use bash $(()) for the calculation

START=$(date "+%s")

work ... work... work ...

END=$(date "+%s")

echo $((END-START))
1
  • Without calling date in bash 4.2+: printf -v START '%(%s)T' -1 – Kusalananda May 26 '20 at 14:25
0

Additionally: time bash script.sh

1
  • In fact I wanted to send the time taken by email after execution is finish. Though this command is useful too. – Keshav Boodhun May 29 '20 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.