How to use a timer in bash?
up vote
27
down vote
favorite
I needed a timer which will start at the very beginning of the script and stops at the end.
bash
add a comment |
up vote
27
down vote
favorite
I needed a timer which will start at the very beginning of the script and stops at the end.
bash
2
please, write a goal you want to achieve. to measure script working time usetimecommand ( time ./script.sh ), to printout current time usedatecommand and so on.
– rush
Nov 4 '12 at 21:06
stackoverflow.com/questions/3840558/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Aug 11 '15 at 13:24
add a comment |
up vote
27
down vote
favorite
up vote
27
down vote
favorite
I needed a timer which will start at the very beginning of the script and stops at the end.
bash
I needed a timer which will start at the very beginning of the script and stops at the end.
bash
bash
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 usetimecommand ( time ./script.sh ), to printout current time usedatecommand and so on.
– rush
Nov 4 '12 at 21:06
stackoverflow.com/questions/3840558/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Aug 11 '15 at 13:24
add a comment |
2
please, write a goal you want to achieve. to measure script working time usetimecommand ( time ./script.sh ), to printout current time usedatecommand 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
add a comment |
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 ))
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
add a comment |
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
Know thetimeyou are using. hackernoon.com/…
– ogc-nick
Mar 9 '17 at 18:32
add a comment |
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
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
add a comment |
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}'
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
add a comment |
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 ))
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
add a comment |
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 ))
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
add a comment |
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 ))
If you want the duration in seconds, at the top use
start=$SECONDS
and at the end
duration=$(( SECONDS - start ))
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
add a comment |
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
add a comment |
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
Know thetimeyou are using. hackernoon.com/…
– ogc-nick
Mar 9 '17 at 18:32
add a comment |
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
Know thetimeyou are using. hackernoon.com/…
– ogc-nick
Mar 9 '17 at 18:32
add a comment |
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
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
edited Nov 4 '12 at 23:11
Michael Mrozek♦
60k28187208
60k28187208
answered Nov 4 '12 at 21:08
Marius Cotofana
631410
631410
Know thetimeyou are using. hackernoon.com/…
– ogc-nick
Mar 9 '17 at 18:32
add a comment |
Know thetimeyou 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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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}'
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
add a comment |
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}'
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
add a comment |
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}'
#!/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}'
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
please, write a goal you want to achieve. to measure script working time use
timecommand ( time ./script.sh ), to printout current time usedatecommand and so on.– rush
Nov 4 '12 at 21:06
stackoverflow.com/questions/3840558/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Aug 11 '15 at 13:24