Bash: Timer in while loop












1














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?










share|improve this question









New contributor




user95138 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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


















1














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?










share|improve this question









New contributor




user95138 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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
















1












1








1







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?










share|improve this question









New contributor




user95138 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




user95138 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user95138 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 31 mins ago









Rui F Ribeiro

39k1479129




39k1479129






New contributor




user95138 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









user95138

61




61




New contributor




user95138 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user95138 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user95138 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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










  • 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












2 Answers
2






active

oldest

votes


















3














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:




  1. changed the sh-bang line from /bin/sh to /bin/bash

  2. added a second condition to the while test to compare $SECONDS to 50

  3. 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.






share|improve this answer





























    3














    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





    share|improve this answer























      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.










      draft saved

      draft discarded


















      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









      3














      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:




      1. changed the sh-bang line from /bin/sh to /bin/bash

      2. added a second condition to the while test to compare $SECONDS to 50

      3. 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.






      share|improve this answer


























        3














        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:




        1. changed the sh-bang line from /bin/sh to /bin/bash

        2. added a second condition to the while test to compare $SECONDS to 50

        3. 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.






        share|improve this answer
























          3












          3








          3






          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:




          1. changed the sh-bang line from /bin/sh to /bin/bash

          2. added a second condition to the while test to compare $SECONDS to 50

          3. 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.






          share|improve this answer












          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:




          1. changed the sh-bang line from /bin/sh to /bin/bash

          2. added a second condition to the while test to compare $SECONDS to 50

          3. 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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          Jeff Schaller

          38.7k1053125




          38.7k1053125

























              3














              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





              share|improve this answer




























                3














                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





                share|improve this answer


























                  3












                  3








                  3






                  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





                  share|improve this answer














                  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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 1 hour ago

























                  answered 1 hour ago









                  nst0022

                  3113




                  3113






















                      user95138 is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      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.




                      draft saved


                      draft discarded














                      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





















































                      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

                      Plaza Victoria

                      Puebla de Zaragoza

                      Musa