How to use a timer in bash?
I needed a timer which will start at the very beginning of the script and stops at the end.
bash
add a comment |
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
Use basic linux cmd:time yourprogram.sh, that @Marius Cotofana explained
– Martijn van Wezel
Jan 4 at 17:13
add a comment |
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 '18 at 19:41
Rui F Ribeiro
40.3k1479137
40.3k1479137
asked Nov 4 '12 at 20:40
MiNdFrEaKMiNdFrEaK
70951423
70951423
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
Use basic linux cmd:time yourprogram.sh, that @Marius Cotofana explained
– Martijn van Wezel
Jan 4 at 17:13
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
Use basic linux cmd:time yourprogram.sh, that @Marius Cotofana explained
– Martijn van Wezel
Jan 4 at 17:13
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
Use basic linux cmd:
time yourprogram.sh, that @Marius Cotofana explained– Martijn van Wezel
Jan 4 at 17:13
Use basic linux cmd:
time yourprogram.sh, that @Marius Cotofana explained– Martijn van Wezel
Jan 4 at 17:13
add a comment |
4 Answers
4
active
oldest
votes
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 |
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
1
Know thetimeyou are using. hackernoon.com/…
– km6zla
Mar 9 '17 at 18:32
add a comment |
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.
Bash has a built-in seconds timer in the form of an internal variable named SECONDS(see: here for other internal variables)
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
3
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 |
#!/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 '18 at 21:44
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 jackmanglenn jackman
51.7k572111
51.7k572111
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 |
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
1
Know thetimeyou are using. hackernoon.com/…
– km6zla
Mar 9 '17 at 18:32
add a comment |
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
1
Know thetimeyou are using. hackernoon.com/…
– km6zla
Mar 9 '17 at 18:32
add a comment |
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♦
61.5k29191211
61.5k29191211
answered Nov 4 '12 at 21:08
Marius CotofanaMarius Cotofana
6461410
6461410
1
Know thetimeyou are using. hackernoon.com/…
– km6zla
Mar 9 '17 at 18:32
add a comment |
1
Know thetimeyou are using. hackernoon.com/…
– km6zla
Mar 9 '17 at 18:32
1
1
Know the
time you are using. hackernoon.com/…– km6zla
Mar 9 '17 at 18:32
Know the
time you are using. hackernoon.com/…– km6zla
Mar 9 '17 at 18:32
add a comment |
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.
Bash has a built-in seconds timer in the form of an internal variable named SECONDS(see: here for other internal variables)
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
3
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 |
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.
Bash has a built-in seconds timer in the form of an internal variable named SECONDS(see: here for other internal variables)
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
3
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 |
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.
Bash has a built-in seconds timer in the form of an internal variable named SECONDS(see: here for other internal variables)
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.
Bash has a built-in seconds timer in the form of an internal variable named SECONDS(see: here for other internal variables)
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
edited 1 hour ago
answered Mar 28 '17 at 23:56
Philip GibbonsPhilip Gibbons
5113
5113
3
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 |
3
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
3
3
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 |
#!/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 '18 at 21:44
add a comment |
#!/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 '18 at 21:44
add a comment |
#!/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 '18 at 21:21
anaskanask
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 '18 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 '18 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 '18 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 '18 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.
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
Use basic linux cmd:
time yourprogram.sh, that @Marius Cotofana explained– Martijn van Wezel
Jan 4 at 17:13