Page 1 of 1

(Solved) Total run time

Posted: Sun May 02, 2021 12:44 am
by Pecu1iar
This is my current bash script.

Code: Select all

#!/bin/bash
# Write to Log File.
sed -i "/# Program Sync Log/a \ $(date +'%F %T') Program AutoSync starting..." /home/tony/Desktop/Sync-Log
# Open Program noGui.
killall java
cd /home/tony/Desktop/Program
java -jar Program.jar -noGui
# Write to Log File
sed -i "/# Program Sync Log/a \ $(date +'%F %T') Program AutoSync complete!" /home/tony/Desktop/Sync-Log
My log file contains the start and finish time. I would like to add a total runtime to this log file sot hat it reads something like "Total Run Time = 25 min, 30 sec."

How do I do this?

Re: Total run time

Posted: Sun May 02, 2021 2:40 pm
by rene
Something like

Code: Select all

#!/bin/bash
# Write to Log File.
START=$(date +'%F %T')
sed -i "/# Program Sync Log/a \ $START Program AutoSync starting..." /home/tony/Desktop/Sync-Log 
# Open Program noGui.
killall java 
cd /home/tony/Desktop/Program 
java -jar Program.jar -noGui 
# Write to Log File
END=$(date +'%F %T')
sed -i "/# Program Sync Log/a \ $END Program AutoSync complete!" /home/tony/Desktop/Sync-Log 
SEC=$(($(date -d "$END" "+%s") - $(date -d "$START" "+%s")))
MIN=$((SEC / 60))
SEC=$((SEC - MIN * 60))
sed -i "/# Program Sync Log/a \ Total Run Time = $MIN min, $SEC sec." /home/tony/Desktop/Sync-Log 

Re: Total run time

Posted: Tue May 11, 2021 8:22 am
by ianblakeley
In my scripts where I need this info I do something like this

Code: Select all

#!/bin/bash
Start="$(date +%s)"

##
#do stuff
##

#Calculate runtime
End="$(date +%s)"
dt="$(echo "$End - $Start" | bc)"
dd="$(echo "$dt/86400" | bc)"
dt2="$(echo "$dt-86400*$dd" | bc)"
dh="$(echo "$dt2/3600" | bc)"
dt3="$(echo "$dt2-3600*$dh" | bc)"
dm="$(echo "$dt3/60" | bc)"
ds="$(echo "$dt3-60*$dm" | bc)"

printf "Total Runtime : %d Days, %02d Hours, %02d Minutes, %02.2f Seconds\n" "$dd" "$dh" "$dm" "$ds" >> logfile.Log
exit

Re: Total run time

Posted: Wed May 12, 2021 5:45 pm
by Pecu1iar
rene wrote:
Sun May 02, 2021 2:40 pm
Something like

Code: Select all

#!/bin/bash
# Write to Log File.
START=$(date +'%F %T')
sed -i "/# Program Sync Log/a \ $START Program AutoSync starting..." /home/tony/Desktop/Sync-Log 
# Open Program noGui.
killall java 
cd /home/tony/Desktop/Program 
java -jar Program.jar -noGui 
# Write to Log File
END=$(date +'%F %T')
sed -i "/# Program Sync Log/a \ $END Program AutoSync complete!" /home/tony/Desktop/Sync-Log 
SEC=$(($(date -d "$END" "+%s") - $(date -d "$START" "+%s")))
MIN=$((SEC / 60))
SEC=$((SEC - MIN * 60))
sed -i "/# Program Sync Log/a \ Total Run Time = $MIN min, $SEC sec." /home/tony/Desktop/Sync-Log 
Thank you, worked like a charm.

Re: (Solved) Total run time

Posted: Mon Jun 07, 2021 10:14 pm
by Termy
There's a special variable called $SECONDS which begins incrementing every second from script execution; you can use that, then use basic maths to choose how you want the user informed. If you need the variable to increment after a certain point, reassign it to 0 at the desired point.