Bash: Timer in while loop
I have a while loop in my script which waits for the connection get online and then continues.
#!/bin/sh
while ! ping -c1 $1 &>/dev/null
do echo "Ping Fail - `date`"
done
echo "Host Found - `date`"
It takes 25 to 45 seconds for the connection to reconnect. I cannot let it wait for any longer than 50 seconds. What would be the best solution to limit the time while loop works?
bash shell-script command-line scripting
New contributor
add a comment |
I have a while loop in my script which waits for the connection get online and then continues.
#!/bin/sh
while ! ping -c1 $1 &>/dev/null
do echo "Ping Fail - `date`"
done
echo "Host Found - `date`"
It takes 25 to 45 seconds for the connection to reconnect. I cannot let it wait for any longer than 50 seconds. What would be the best solution to limit the time while loop works?
bash shell-script command-line scripting
New contributor
understanding that the pings may still be failing after 50 seconds...?
– Jeff Schaller
1 hour ago
Yes, this is exactly what I mean
– user95138
1 hour ago
You are dealing with ping timeouts too
– Rui F Ribeiro
30 mins ago
add a comment |
I have a while loop in my script which waits for the connection get online and then continues.
#!/bin/sh
while ! ping -c1 $1 &>/dev/null
do echo "Ping Fail - `date`"
done
echo "Host Found - `date`"
It takes 25 to 45 seconds for the connection to reconnect. I cannot let it wait for any longer than 50 seconds. What would be the best solution to limit the time while loop works?
bash shell-script command-line scripting
New contributor
I have a while loop in my script which waits for the connection get online and then continues.
#!/bin/sh
while ! ping -c1 $1 &>/dev/null
do echo "Ping Fail - `date`"
done
echo "Host Found - `date`"
It takes 25 to 45 seconds for the connection to reconnect. I cannot let it wait for any longer than 50 seconds. What would be the best solution to limit the time while loop works?
bash shell-script command-line scripting
bash shell-script command-line scripting
New contributor
New contributor
edited 31 mins ago
Rui F Ribeiro
39k1479129
39k1479129
New contributor
asked 1 hour ago
user95138
61
61
New contributor
New contributor
understanding that the pings may still be failing after 50 seconds...?
– Jeff Schaller
1 hour ago
Yes, this is exactly what I mean
– user95138
1 hour ago
You are dealing with ping timeouts too
– Rui F Ribeiro
30 mins ago
add a comment |
understanding that the pings may still be failing after 50 seconds...?
– Jeff Schaller
1 hour ago
Yes, this is exactly what I mean
– user95138
1 hour ago
You are dealing with ping timeouts too
– Rui F Ribeiro
30 mins ago
understanding that the pings may still be failing after 50 seconds...?
– Jeff Schaller
1 hour ago
understanding that the pings may still be failing after 50 seconds...?
– Jeff Schaller
1 hour ago
Yes, this is exactly what I mean
– user95138
1 hour ago
Yes, this is exactly what I mean
– user95138
1 hour ago
You are dealing with ping timeouts too
– Rui F Ribeiro
30 mins ago
You are dealing with ping timeouts too
– Rui F Ribeiro
30 mins ago
add a comment |
2 Answers
2
active
oldest
votes
A rough cut at it would be to use the bash special variable $SECONDS, which counts the number of seconds since the shell started. I've made three changes to the script:
- changed the sh-bang line from /bin/sh to /bin/bash
- added a second condition to the
while
test to compare $SECONDS to 50 - quoted
$1
The new script:
#!/bin/bash
while ! ping -c1 "$1" &>/dev/null; [[ "$SECONDS" -lt 50 ]]
do echo "Ping Fail - `date`"
done
echo "Host Found - `date`"
I would just note that the Host Found
statement is potentially misleading in the case of a 50-second timeout. You could compare $SECONDS to 50 after the loop to determine whether the timeout occurred.
This is a rough estimate of 50 seconds, since the loop could be entered with $SECONDS == 49, and then ping
could take more than one second to succeed or fail.
add a comment |
Without a while loop:
# -W 50 = timeout after 50 seconds
# -c 1 = 1 packet to be sent
response="$(ping -W 50 -c 1 "$1" | grep '1
packets transmitted, 1 received')"
if [ "$response" == '' ] ; then
echo no response after 50 seconds
else
echo connected
fi
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
});
}
});
user95138 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f491773%2fbash-timer-in-while-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
A rough cut at it would be to use the bash special variable $SECONDS, which counts the number of seconds since the shell started. I've made three changes to the script:
- changed the sh-bang line from /bin/sh to /bin/bash
- added a second condition to the
while
test to compare $SECONDS to 50 - quoted
$1
The new script:
#!/bin/bash
while ! ping -c1 "$1" &>/dev/null; [[ "$SECONDS" -lt 50 ]]
do echo "Ping Fail - `date`"
done
echo "Host Found - `date`"
I would just note that the Host Found
statement is potentially misleading in the case of a 50-second timeout. You could compare $SECONDS to 50 after the loop to determine whether the timeout occurred.
This is a rough estimate of 50 seconds, since the loop could be entered with $SECONDS == 49, and then ping
could take more than one second to succeed or fail.
add a comment |
A rough cut at it would be to use the bash special variable $SECONDS, which counts the number of seconds since the shell started. I've made three changes to the script:
- changed the sh-bang line from /bin/sh to /bin/bash
- added a second condition to the
while
test to compare $SECONDS to 50 - quoted
$1
The new script:
#!/bin/bash
while ! ping -c1 "$1" &>/dev/null; [[ "$SECONDS" -lt 50 ]]
do echo "Ping Fail - `date`"
done
echo "Host Found - `date`"
I would just note that the Host Found
statement is potentially misleading in the case of a 50-second timeout. You could compare $SECONDS to 50 after the loop to determine whether the timeout occurred.
This is a rough estimate of 50 seconds, since the loop could be entered with $SECONDS == 49, and then ping
could take more than one second to succeed or fail.
add a comment |
A rough cut at it would be to use the bash special variable $SECONDS, which counts the number of seconds since the shell started. I've made three changes to the script:
- changed the sh-bang line from /bin/sh to /bin/bash
- added a second condition to the
while
test to compare $SECONDS to 50 - quoted
$1
The new script:
#!/bin/bash
while ! ping -c1 "$1" &>/dev/null; [[ "$SECONDS" -lt 50 ]]
do echo "Ping Fail - `date`"
done
echo "Host Found - `date`"
I would just note that the Host Found
statement is potentially misleading in the case of a 50-second timeout. You could compare $SECONDS to 50 after the loop to determine whether the timeout occurred.
This is a rough estimate of 50 seconds, since the loop could be entered with $SECONDS == 49, and then ping
could take more than one second to succeed or fail.
A rough cut at it would be to use the bash special variable $SECONDS, which counts the number of seconds since the shell started. I've made three changes to the script:
- changed the sh-bang line from /bin/sh to /bin/bash
- added a second condition to the
while
test to compare $SECONDS to 50 - quoted
$1
The new script:
#!/bin/bash
while ! ping -c1 "$1" &>/dev/null; [[ "$SECONDS" -lt 50 ]]
do echo "Ping Fail - `date`"
done
echo "Host Found - `date`"
I would just note that the Host Found
statement is potentially misleading in the case of a 50-second timeout. You could compare $SECONDS to 50 after the loop to determine whether the timeout occurred.
This is a rough estimate of 50 seconds, since the loop could be entered with $SECONDS == 49, and then ping
could take more than one second to succeed or fail.
answered 1 hour ago
Jeff Schaller
38.7k1053125
38.7k1053125
add a comment |
add a comment |
Without a while loop:
# -W 50 = timeout after 50 seconds
# -c 1 = 1 packet to be sent
response="$(ping -W 50 -c 1 "$1" | grep '1
packets transmitted, 1 received')"
if [ "$response" == '' ] ; then
echo no response after 50 seconds
else
echo connected
fi
add a comment |
Without a while loop:
# -W 50 = timeout after 50 seconds
# -c 1 = 1 packet to be sent
response="$(ping -W 50 -c 1 "$1" | grep '1
packets transmitted, 1 received')"
if [ "$response" == '' ] ; then
echo no response after 50 seconds
else
echo connected
fi
add a comment |
Without a while loop:
# -W 50 = timeout after 50 seconds
# -c 1 = 1 packet to be sent
response="$(ping -W 50 -c 1 "$1" | grep '1
packets transmitted, 1 received')"
if [ "$response" == '' ] ; then
echo no response after 50 seconds
else
echo connected
fi
Without a while loop:
# -W 50 = timeout after 50 seconds
# -c 1 = 1 packet to be sent
response="$(ping -W 50 -c 1 "$1" | grep '1
packets transmitted, 1 received')"
if [ "$response" == '' ] ; then
echo no response after 50 seconds
else
echo connected
fi
edited 1 hour ago
answered 1 hour ago
nst0022
3113
3113
add a comment |
add a comment |
user95138 is a new contributor. Be nice, and check out our Code of Conduct.
user95138 is a new contributor. Be nice, and check out our Code of Conduct.
user95138 is a new contributor. Be nice, and check out our Code of Conduct.
user95138 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f491773%2fbash-timer-in-while-loop%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
understanding that the pings may still be failing after 50 seconds...?
– Jeff Schaller
1 hour ago
Yes, this is exactly what I mean
– user95138
1 hour ago
You are dealing with ping timeouts too
– Rui F Ribeiro
30 mins ago