(Solved) Total run time

About writing shell scripts and making the most of your shell
Forum rules
Before you post please read how to get help
Post Reply
Pecu1iar
Level 1
Level 1
Posts: 19
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 Pecu1iar on Wed May 12, 2021 5:46 pm, edited 1 time in total.
rene
Level 17
Level 17
Posts: 7556
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: 41
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: 19
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 7
Level 7
Posts: 1621
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 use Linux Mint 18.3 with Cinnamon in a VirtualBox VM for testing & sandboxing.

I'm Terminalforlife (TFL/LL) on YouTube: https://www.youtube.com/c/terminalforlife
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife
Post Reply