(Solved) Total run time

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Pecu1iar
Level 1
Level 1
Posts: 39
Joined: Sun Nov 01, 2020 6:14 pm

(Solved) Total run time

Post 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?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Total run time

Post 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 
ianblakeley
Level 1
Level 1
Posts: 42
Joined: Wed Jan 18, 2017 11:47 pm
Location: Penang
Contact:

Re: Total run time

Post 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
--
Ian
Pecu1iar
Level 1
Level 1
Posts: 39
Joined: Sun Nov 01, 2020 6:14 pm

Re: Total run time

Post 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.
User avatar
Termy
Level 12
Level 12
Posts: 4254
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: (Solved) Total run time

Post 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.
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”