How to use a timer in bash?











up vote
27
down vote

favorite
5












I needed a timer which will start at the very beginning of the script and stops at the end.










share|improve this question




















  • 2




    please, write a goal you want to achieve. to measure script working time use time command ( time ./script.sh ), to printout current time use date command and so on.
    – rush
    Nov 4 '12 at 21:06










  • stackoverflow.com/questions/3840558/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Aug 11 '15 at 13:24















up vote
27
down vote

favorite
5












I needed a timer which will start at the very beginning of the script and stops at the end.










share|improve this question




















  • 2




    please, write a goal you want to achieve. to measure script working time use time command ( time ./script.sh ), to printout current time use date command and so on.
    – rush
    Nov 4 '12 at 21:06










  • stackoverflow.com/questions/3840558/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Aug 11 '15 at 13:24













up vote
27
down vote

favorite
5









up vote
27
down vote

favorite
5






5





I needed a timer which will start at the very beginning of the script and stops at the end.










share|improve this question















I needed a timer which will start at the very beginning of the script and stops at the end.







bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 at 19:41









Rui F Ribeiro

38.3k1476127




38.3k1476127










asked Nov 4 '12 at 20:40









MiNdFrEaK

69451423




69451423








  • 2




    please, write a goal you want to achieve. to measure script working time use time command ( time ./script.sh ), to printout current time use date command and so on.
    – rush
    Nov 4 '12 at 21:06










  • stackoverflow.com/questions/3840558/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Aug 11 '15 at 13:24














  • 2




    please, write a goal you want to achieve. to measure script working time use time command ( time ./script.sh ), to printout current time use date command and so on.
    – rush
    Nov 4 '12 at 21:06










  • stackoverflow.com/questions/3840558/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Aug 11 '15 at 13:24








2




2




please, write a goal you want to achieve. to measure script working time use time command ( time ./script.sh ), to printout current time use date command and so on.
– rush
Nov 4 '12 at 21:06




please, write a goal you want to achieve. to measure script working time use time command ( time ./script.sh ), to printout current time use date command and so on.
– rush
Nov 4 '12 at 21:06












stackoverflow.com/questions/3840558/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Aug 11 '15 at 13:24




stackoverflow.com/questions/3840558/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Aug 11 '15 at 13:24










4 Answers
4






active

oldest

votes

















up vote
27
down vote



accepted










If you want the duration in seconds, at the top use



start=$SECONDS


and at the end



duration=$(( SECONDS - start ))





share|improve this answer





















  • What if it takes more than seconds?
    – Pithikos
    Oct 17 '14 at 16:49










  • @Pithikos See Philip Gibbons's answer below
    – xhienne
    Mar 29 '17 at 0:37


















up vote
20
down vote













You could use Linux's built-in time command. From the man page:




time COMMAND [arguments]



time determines which information to display about the resources used by the COMMAND from the string FORMAT. If no format is specified on the command line, but the TIME environment variable is set, its value is used as the format.




To time the cleanup.sh script, use:



$ time cleanup.sh





share|improve this answer























  • Know the time you are using. hackernoon.com/…
    – ogc-nick
    Mar 9 '17 at 18:32


















up vote
5
down vote













I realise this is a pretty old question but this is the way I do it for anything when I want/need to know how long something took to run.



Put SECONDS=0 before whatever you're checking the run time of. It needs to be after any user input to get a good result so if you're prompting for information, put it after the prompt(s).



Then, put this at the end of what you're checking:



if (( $SECONDS > 3600 )) ; then
let "hours=SECONDS/3600"
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
elif (( $SECONDS > 60 )) ; then
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $minutes minute(s) and $seconds second(s)"
else
echo "Completed in $SECONDS seconds"
fi


If you only want to know the time in seconds, you can simplify the above to:



echo "Completed in $SECONDS seconds"


As an example:



read -rp "What's your name? " "name"
SECONDS=0
echo "Hello, $name"
if (( $SECONDS > 3600 )) ; then
let "hours=SECONDS/3600"
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
elif (( $SECONDS > 60 )) ; then
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $minutes minute(s) and $seconds second(s)"
else
echo "Completed in $SECONDS seconds"
fi





share|improve this answer

















  • 2




    A shortcut for those who don't want such an elaborate time formatting: date +%T -d "1/1 + $SECONDS sec" (limited to less than 24h, but you can adapt to add days too)
    – xhienne
    Mar 29 '17 at 0:40




















up vote
0
down vote













#!/bin/bash

start=$(date +%s)
#
# do something
sleep 10
#
#
end=$(date +%s)

seconds=$(echo "$end - $start" | bc)
echo $seconds' sec'

echo 'Formatted:'
awk -v t=$seconds 'BEGIN{t=int(t*1000); printf "%d:%02d:%02dn", t/3600000, t/60000%60, t/1000%60}'





share|improve this answer

















  • 1




    Hi, welcome on the Unix SE! Code-only answers don't look very well, maybe you could explain what your script is doing.
    – peterh
    Jun 14 at 21:44











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f53841%2fhow-to-use-a-timer-in-bash%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
27
down vote



accepted










If you want the duration in seconds, at the top use



start=$SECONDS


and at the end



duration=$(( SECONDS - start ))





share|improve this answer





















  • What if it takes more than seconds?
    – Pithikos
    Oct 17 '14 at 16:49










  • @Pithikos See Philip Gibbons's answer below
    – xhienne
    Mar 29 '17 at 0:37















up vote
27
down vote



accepted










If you want the duration in seconds, at the top use



start=$SECONDS


and at the end



duration=$(( SECONDS - start ))





share|improve this answer





















  • What if it takes more than seconds?
    – Pithikos
    Oct 17 '14 at 16:49










  • @Pithikos See Philip Gibbons's answer below
    – xhienne
    Mar 29 '17 at 0:37













up vote
27
down vote



accepted







up vote
27
down vote



accepted






If you want the duration in seconds, at the top use



start=$SECONDS


and at the end



duration=$(( SECONDS - start ))





share|improve this answer












If you want the duration in seconds, at the top use



start=$SECONDS


and at the end



duration=$(( SECONDS - start ))






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 4 '12 at 22:32









glenn jackman

49.6k569106




49.6k569106












  • What if it takes more than seconds?
    – Pithikos
    Oct 17 '14 at 16:49










  • @Pithikos See Philip Gibbons's answer below
    – xhienne
    Mar 29 '17 at 0:37


















  • What if it takes more than seconds?
    – Pithikos
    Oct 17 '14 at 16:49










  • @Pithikos See Philip Gibbons's answer below
    – xhienne
    Mar 29 '17 at 0:37
















What if it takes more than seconds?
– Pithikos
Oct 17 '14 at 16:49




What if it takes more than seconds?
– Pithikos
Oct 17 '14 at 16:49












@Pithikos See Philip Gibbons's answer below
– xhienne
Mar 29 '17 at 0:37




@Pithikos See Philip Gibbons's answer below
– xhienne
Mar 29 '17 at 0:37












up vote
20
down vote













You could use Linux's built-in time command. From the man page:




time COMMAND [arguments]



time determines which information to display about the resources used by the COMMAND from the string FORMAT. If no format is specified on the command line, but the TIME environment variable is set, its value is used as the format.




To time the cleanup.sh script, use:



$ time cleanup.sh





share|improve this answer























  • Know the time you are using. hackernoon.com/…
    – ogc-nick
    Mar 9 '17 at 18:32















up vote
20
down vote













You could use Linux's built-in time command. From the man page:




time COMMAND [arguments]



time determines which information to display about the resources used by the COMMAND from the string FORMAT. If no format is specified on the command line, but the TIME environment variable is set, its value is used as the format.




To time the cleanup.sh script, use:



$ time cleanup.sh





share|improve this answer























  • Know the time you are using. hackernoon.com/…
    – ogc-nick
    Mar 9 '17 at 18:32













up vote
20
down vote










up vote
20
down vote









You could use Linux's built-in time command. From the man page:




time COMMAND [arguments]



time determines which information to display about the resources used by the COMMAND from the string FORMAT. If no format is specified on the command line, but the TIME environment variable is set, its value is used as the format.




To time the cleanup.sh script, use:



$ time cleanup.sh





share|improve this answer














You could use Linux's built-in time command. From the man page:




time COMMAND [arguments]



time determines which information to display about the resources used by the COMMAND from the string FORMAT. If no format is specified on the command line, but the TIME environment variable is set, its value is used as the format.




To time the cleanup.sh script, use:



$ time cleanup.sh






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 4 '12 at 23:11









Michael Mrozek

60k28187208




60k28187208










answered Nov 4 '12 at 21:08









Marius Cotofana

631410




631410












  • Know the time you are using. hackernoon.com/…
    – ogc-nick
    Mar 9 '17 at 18:32


















  • Know the time you are using. hackernoon.com/…
    – ogc-nick
    Mar 9 '17 at 18:32
















Know the time you are using. hackernoon.com/…
– ogc-nick
Mar 9 '17 at 18:32




Know the time you are using. hackernoon.com/…
– ogc-nick
Mar 9 '17 at 18:32










up vote
5
down vote













I realise this is a pretty old question but this is the way I do it for anything when I want/need to know how long something took to run.



Put SECONDS=0 before whatever you're checking the run time of. It needs to be after any user input to get a good result so if you're prompting for information, put it after the prompt(s).



Then, put this at the end of what you're checking:



if (( $SECONDS > 3600 )) ; then
let "hours=SECONDS/3600"
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
elif (( $SECONDS > 60 )) ; then
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $minutes minute(s) and $seconds second(s)"
else
echo "Completed in $SECONDS seconds"
fi


If you only want to know the time in seconds, you can simplify the above to:



echo "Completed in $SECONDS seconds"


As an example:



read -rp "What's your name? " "name"
SECONDS=0
echo "Hello, $name"
if (( $SECONDS > 3600 )) ; then
let "hours=SECONDS/3600"
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
elif (( $SECONDS > 60 )) ; then
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $minutes minute(s) and $seconds second(s)"
else
echo "Completed in $SECONDS seconds"
fi





share|improve this answer

















  • 2




    A shortcut for those who don't want such an elaborate time formatting: date +%T -d "1/1 + $SECONDS sec" (limited to less than 24h, but you can adapt to add days too)
    – xhienne
    Mar 29 '17 at 0:40

















up vote
5
down vote













I realise this is a pretty old question but this is the way I do it for anything when I want/need to know how long something took to run.



Put SECONDS=0 before whatever you're checking the run time of. It needs to be after any user input to get a good result so if you're prompting for information, put it after the prompt(s).



Then, put this at the end of what you're checking:



if (( $SECONDS > 3600 )) ; then
let "hours=SECONDS/3600"
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
elif (( $SECONDS > 60 )) ; then
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $minutes minute(s) and $seconds second(s)"
else
echo "Completed in $SECONDS seconds"
fi


If you only want to know the time in seconds, you can simplify the above to:



echo "Completed in $SECONDS seconds"


As an example:



read -rp "What's your name? " "name"
SECONDS=0
echo "Hello, $name"
if (( $SECONDS > 3600 )) ; then
let "hours=SECONDS/3600"
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
elif (( $SECONDS > 60 )) ; then
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $minutes minute(s) and $seconds second(s)"
else
echo "Completed in $SECONDS seconds"
fi





share|improve this answer

















  • 2




    A shortcut for those who don't want such an elaborate time formatting: date +%T -d "1/1 + $SECONDS sec" (limited to less than 24h, but you can adapt to add days too)
    – xhienne
    Mar 29 '17 at 0:40















up vote
5
down vote










up vote
5
down vote









I realise this is a pretty old question but this is the way I do it for anything when I want/need to know how long something took to run.



Put SECONDS=0 before whatever you're checking the run time of. It needs to be after any user input to get a good result so if you're prompting for information, put it after the prompt(s).



Then, put this at the end of what you're checking:



if (( $SECONDS > 3600 )) ; then
let "hours=SECONDS/3600"
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
elif (( $SECONDS > 60 )) ; then
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $minutes minute(s) and $seconds second(s)"
else
echo "Completed in $SECONDS seconds"
fi


If you only want to know the time in seconds, you can simplify the above to:



echo "Completed in $SECONDS seconds"


As an example:



read -rp "What's your name? " "name"
SECONDS=0
echo "Hello, $name"
if (( $SECONDS > 3600 )) ; then
let "hours=SECONDS/3600"
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
elif (( $SECONDS > 60 )) ; then
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $minutes minute(s) and $seconds second(s)"
else
echo "Completed in $SECONDS seconds"
fi





share|improve this answer












I realise this is a pretty old question but this is the way I do it for anything when I want/need to know how long something took to run.



Put SECONDS=0 before whatever you're checking the run time of. It needs to be after any user input to get a good result so if you're prompting for information, put it after the prompt(s).



Then, put this at the end of what you're checking:



if (( $SECONDS > 3600 )) ; then
let "hours=SECONDS/3600"
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
elif (( $SECONDS > 60 )) ; then
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $minutes minute(s) and $seconds second(s)"
else
echo "Completed in $SECONDS seconds"
fi


If you only want to know the time in seconds, you can simplify the above to:



echo "Completed in $SECONDS seconds"


As an example:



read -rp "What's your name? " "name"
SECONDS=0
echo "Hello, $name"
if (( $SECONDS > 3600 )) ; then
let "hours=SECONDS/3600"
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
elif (( $SECONDS > 60 )) ; then
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo "Completed in $minutes minute(s) and $seconds second(s)"
else
echo "Completed in $SECONDS seconds"
fi






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 '17 at 23:56









Philip Gibbons

5112




5112








  • 2




    A shortcut for those who don't want such an elaborate time formatting: date +%T -d "1/1 + $SECONDS sec" (limited to less than 24h, but you can adapt to add days too)
    – xhienne
    Mar 29 '17 at 0:40
















  • 2




    A shortcut for those who don't want such an elaborate time formatting: date +%T -d "1/1 + $SECONDS sec" (limited to less than 24h, but you can adapt to add days too)
    – xhienne
    Mar 29 '17 at 0:40










2




2




A shortcut for those who don't want such an elaborate time formatting: date +%T -d "1/1 + $SECONDS sec" (limited to less than 24h, but you can adapt to add days too)
– xhienne
Mar 29 '17 at 0:40






A shortcut for those who don't want such an elaborate time formatting: date +%T -d "1/1 + $SECONDS sec" (limited to less than 24h, but you can adapt to add days too)
– xhienne
Mar 29 '17 at 0:40












up vote
0
down vote













#!/bin/bash

start=$(date +%s)
#
# do something
sleep 10
#
#
end=$(date +%s)

seconds=$(echo "$end - $start" | bc)
echo $seconds' sec'

echo 'Formatted:'
awk -v t=$seconds 'BEGIN{t=int(t*1000); printf "%d:%02d:%02dn", t/3600000, t/60000%60, t/1000%60}'





share|improve this answer

















  • 1




    Hi, welcome on the Unix SE! Code-only answers don't look very well, maybe you could explain what your script is doing.
    – peterh
    Jun 14 at 21:44















up vote
0
down vote













#!/bin/bash

start=$(date +%s)
#
# do something
sleep 10
#
#
end=$(date +%s)

seconds=$(echo "$end - $start" | bc)
echo $seconds' sec'

echo 'Formatted:'
awk -v t=$seconds 'BEGIN{t=int(t*1000); printf "%d:%02d:%02dn", t/3600000, t/60000%60, t/1000%60}'





share|improve this answer

















  • 1




    Hi, welcome on the Unix SE! Code-only answers don't look very well, maybe you could explain what your script is doing.
    – peterh
    Jun 14 at 21:44













up vote
0
down vote










up vote
0
down vote









#!/bin/bash

start=$(date +%s)
#
# do something
sleep 10
#
#
end=$(date +%s)

seconds=$(echo "$end - $start" | bc)
echo $seconds' sec'

echo 'Formatted:'
awk -v t=$seconds 'BEGIN{t=int(t*1000); printf "%d:%02d:%02dn", t/3600000, t/60000%60, t/1000%60}'





share|improve this answer












#!/bin/bash

start=$(date +%s)
#
# do something
sleep 10
#
#
end=$(date +%s)

seconds=$(echo "$end - $start" | bc)
echo $seconds' sec'

echo 'Formatted:'
awk -v t=$seconds 'BEGIN{t=int(t*1000); printf "%d:%02d:%02dn", t/3600000, t/60000%60, t/1000%60}'






share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 14 at 21:21









anask

314




314








  • 1




    Hi, welcome on the Unix SE! Code-only answers don't look very well, maybe you could explain what your script is doing.
    – peterh
    Jun 14 at 21:44














  • 1




    Hi, welcome on the Unix SE! Code-only answers don't look very well, maybe you could explain what your script is doing.
    – peterh
    Jun 14 at 21:44








1




1




Hi, welcome on the Unix SE! Code-only answers don't look very well, maybe you could explain what your script is doing.
– peterh
Jun 14 at 21:44




Hi, welcome on the Unix SE! Code-only answers don't look very well, maybe you could explain what your script is doing.
– peterh
Jun 14 at 21:44


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f53841%2fhow-to-use-a-timer-in-bash%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List directoties down one level, excluding some named directories and files

list processes belonging to a network namespace

list systemd RuntimeDirectory mounts