[SOLVED] duration, start time and end time of a command after it is finished
by blueray from LinuxQuestions.org on (#5CB6J)
This question was originally posted on https://unix.stackexchange.com/quest...it-is-finished
I want to have a report like the following after a command finishes:
Code:Start Time: 02/01/21 01:27pm
End Time: 02/01/21 02:29pm
Total Duration: 01 Hour 02 MinutesI mainly want to run rsync, youtube-dl, tail -f etc with this command.
The solution I got was
Code:#! /bin/zsh -
zmodload zsh/datetime
TIMEFMT='Total duration: %*E'
strftime -s start 'Start Time: %d/%m/%y %I:%M%P'
strftime -s end 'End Time: %d/%m/%y %I:%M%P'
{
duration=$(
exec 2>&1
time "$@" 2>&3 3>&-
)
} 3>&2
ret=$?
print -rlu2 $start $end $duration
exit $retExample usage:
Code:$ ./reporttime sleep 65
Start Time: 02/01/21 09:26am
End Time: 02/01/21 09:26am
Total duration: 1:05.00It has few deal breaking issues.
1. It does not print the report at the end when the command within has stdout of its own. try:
Code:./reporttime cat reporttime.2. It waits till the whole command is complete, then it shows the whole output at once.
For example, try monitoring a log file with this script.
Code:./reporttime tail -f /var/log/apache2/access.log3. If I use ctrl+c to close a command, it does not show the report. Ex
Code:./reporttime tail -f /var/log/apache2/access.logthen press ctrl+c.
The code I was originally working with was:
Code:start=$(date +"%d/%m/%y %I:%M%P")
start_duration=$(date +%s)
youtube-dl
end=$(date +"%d/%m/%y %I:%M%P")
end_duration=$(date +%s)
runtime=$((end_duration-start_duration))
echo "Start Time: ${start}"
echo "End Time: ${end}"It had few issues of its own.
1. I am not understanding how to print Total Duration: 01 Hour 02 Minutes
2. How can I make this script universal so that I can use any command, not just youtube-dl in this script.
What might be a solution here.


I want to have a report like the following after a command finishes:
Code:Start Time: 02/01/21 01:27pm
End Time: 02/01/21 02:29pm
Total Duration: 01 Hour 02 MinutesI mainly want to run rsync, youtube-dl, tail -f etc with this command.
The solution I got was
Code:#! /bin/zsh -
zmodload zsh/datetime
TIMEFMT='Total duration: %*E'
strftime -s start 'Start Time: %d/%m/%y %I:%M%P'
strftime -s end 'End Time: %d/%m/%y %I:%M%P'
{
duration=$(
exec 2>&1
time "$@" 2>&3 3>&-
)
} 3>&2
ret=$?
print -rlu2 $start $end $duration
exit $retExample usage:
Code:$ ./reporttime sleep 65
Start Time: 02/01/21 09:26am
End Time: 02/01/21 09:26am
Total duration: 1:05.00It has few deal breaking issues.
1. It does not print the report at the end when the command within has stdout of its own. try:
Code:./reporttime cat reporttime.2. It waits till the whole command is complete, then it shows the whole output at once.
For example, try monitoring a log file with this script.
Code:./reporttime tail -f /var/log/apache2/access.log3. If I use ctrl+c to close a command, it does not show the report. Ex
Code:./reporttime tail -f /var/log/apache2/access.logthen press ctrl+c.
The code I was originally working with was:
Code:start=$(date +"%d/%m/%y %I:%M%P")
start_duration=$(date +%s)
youtube-dl
end=$(date +"%d/%m/%y %I:%M%P")
end_duration=$(date +%s)
runtime=$((end_duration-start_duration))
echo "Start Time: ${start}"
echo "End Time: ${end}"It had few issues of its own.
1. I am not understanding how to print Total Duration: 01 Hour 02 Minutes
2. How can I make this script universal so that I can use any command, not just youtube-dl in this script.
What might be a solution here.