Bash ping script file for checking host availability

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite
4












I am trying to write a bash script in a file that would, when run start pinging a host until it becomes available, when the host becomes reachable it runs a command and stops executing, i tried writing one but the script continues pinging until the count ends,



Plus i need to put that process in the background but if i run the script with the $ sign it still runs in foreground,



#!/bin/bash
ping -c30 -i3 192.168.137.163
if [ $? -eq 0 ]
then /root/scripts/test1.sh
exit 0
else echo “fail”
fi


Any help would be appreciated tnx in advance!










share|improve this question

























    up vote
    2
    down vote

    favorite
    4












    I am trying to write a bash script in a file that would, when run start pinging a host until it becomes available, when the host becomes reachable it runs a command and stops executing, i tried writing one but the script continues pinging until the count ends,



    Plus i need to put that process in the background but if i run the script with the $ sign it still runs in foreground,



    #!/bin/bash
    ping -c30 -i3 192.168.137.163
    if [ $? -eq 0 ]
    then /root/scripts/test1.sh
    exit 0
    else echo “fail”
    fi


    Any help would be appreciated tnx in advance!










    share|improve this question























      up vote
      2
      down vote

      favorite
      4









      up vote
      2
      down vote

      favorite
      4






      4





      I am trying to write a bash script in a file that would, when run start pinging a host until it becomes available, when the host becomes reachable it runs a command and stops executing, i tried writing one but the script continues pinging until the count ends,



      Plus i need to put that process in the background but if i run the script with the $ sign it still runs in foreground,



      #!/bin/bash
      ping -c30 -i3 192.168.137.163
      if [ $? -eq 0 ]
      then /root/scripts/test1.sh
      exit 0
      else echo “fail”
      fi


      Any help would be appreciated tnx in advance!










      share|improve this question













      I am trying to write a bash script in a file that would, when run start pinging a host until it becomes available, when the host becomes reachable it runs a command and stops executing, i tried writing one but the script continues pinging until the count ends,



      Plus i need to put that process in the background but if i run the script with the $ sign it still runs in foreground,



      #!/bin/bash
      ping -c30 -i3 192.168.137.163
      if [ $? -eq 0 ]
      then /root/scripts/test1.sh
      exit 0
      else echo “fail”
      fi


      Any help would be appreciated tnx in advance!







      bash ping exit oracle-linux






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 11 '15 at 15:46









      dusan90

      30227




      30227




















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          8
          down vote



          accepted










          I would use this, a simple one-liner:



          while ! ping -c1 HOSTNAME &>/dev/null; do echo "Ping Fail - `date`"; done ; echo "Host Found - `date`" ; /root/scripts/test1.sh


          Replace HOSTNAME with the host you are trying to ping.




          I missed the part about putting it in the background, put that line in a shellscript like so:



          #!/bin/sh

          while ! ping -c1 $1 &>/dev/null
          do echo "Ping Fail - `date`"
          done
          echo "Host Found - `date`"
          /root/scripts/test1.sh


          And to background it you would run it like so:



          nohup ./networktest.sh HOSTNAME > /tmp/networktest.out 2>&1 &


          Again replace HOSTNAME with the host you are trying to ping. In this approach you are passing the hostname as an argument to the shellscript.



          Just as a general warning, if your host stays down, you will have this script continuously pinging in the background until you either kill it or the host is found. So I would keep that in mind when you run this. Because you could end up eating system resources if you forget about this.






          share|improve this answer






















          • Tnx for the answer and the warning, this is a visualized environment so the script will start after the physical servers boot so the VM's will start certainly, but will keep in mind if i delete a VM, tnx again
            – dusan90
            Feb 12 '15 at 9:18











          • In particular by logging the failures every time, and storing all the output in a file in /tmp, if the host goes down you'll end up filling /tmp. That tends to be bad news...
            – Stephen Kitt
            Feb 13 '15 at 16:57

















          up vote
          4
          down vote













          By passing the parameters '-c 30' to ping, it will try 30 ping and stop. It will check after if the command succeeds. I think it is best to do a loop that contains one ping and check if this ping succeed. Something like that:



          while true;
          do
          ping -c1 google.com
          if [ $? -eq 0 ]
          then
          /root/scripts/test1.sh
          exit 0
          fi
          done


          If by still running on the foreground, you mean it is still printing on the terminal, you can redirect stdin and stdout to /dev/null .
          Hope it helps






          share|improve this answer






















          • By unlocking the prompt so i could work on something else, tnx for help
            – dusan90
            Feb 12 '15 at 9:17

















          up vote
          1
          down vote













          ping -oc 100000 Hostname > /dev/null && /root/scripts/test1.sh 



          • ping -o exits the ping after the first packet is received


          • > /dev/null redirects the output, so you won't see it


          • && would run the next command, if the previous command ere successful

          In addition, you can run any process in the background by adding & to the end of it; for example, echo "123" & will run in the background






          share|improve this answer



























            up vote
            0
            down vote













            An old post, but as a suggestion you can use the -w option on ping to avoid the loop. For example,



            ping -w 30 -c 1 host


            will try for 30 seconds with one ping per second (default ping has 1 second interval between pings) and will exit on the first successful ping.



            If you don't need a timeout, I.e. wait for ever, just use a very large value with -w.






            share|improve this answer





























              up vote
              0
              down vote













              is there any script like, i need to ping from my Ubuntu Virtual Machine to Other Virtual Machine with time interval 30 minutes automatically. It will do 5 ping and stop ping my server then again ping command will automatically generate after 30 minutes.



              Md Nazmus Sakib
              IT Administrator
              mail:nazmussakib021@gmail.com





              share








              New contributor




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

















                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: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                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%2f184266%2fbash-ping-script-file-for-checking-host-availability%23new-answer', 'question_page');

                );

                Post as a guest






























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                8
                down vote



                accepted










                I would use this, a simple one-liner:



                while ! ping -c1 HOSTNAME &>/dev/null; do echo "Ping Fail - `date`"; done ; echo "Host Found - `date`" ; /root/scripts/test1.sh


                Replace HOSTNAME with the host you are trying to ping.




                I missed the part about putting it in the background, put that line in a shellscript like so:



                #!/bin/sh

                while ! ping -c1 $1 &>/dev/null
                do echo "Ping Fail - `date`"
                done
                echo "Host Found - `date`"
                /root/scripts/test1.sh


                And to background it you would run it like so:



                nohup ./networktest.sh HOSTNAME > /tmp/networktest.out 2>&1 &


                Again replace HOSTNAME with the host you are trying to ping. In this approach you are passing the hostname as an argument to the shellscript.



                Just as a general warning, if your host stays down, you will have this script continuously pinging in the background until you either kill it or the host is found. So I would keep that in mind when you run this. Because you could end up eating system resources if you forget about this.






                share|improve this answer






















                • Tnx for the answer and the warning, this is a visualized environment so the script will start after the physical servers boot so the VM's will start certainly, but will keep in mind if i delete a VM, tnx again
                  – dusan90
                  Feb 12 '15 at 9:18











                • In particular by logging the failures every time, and storing all the output in a file in /tmp, if the host goes down you'll end up filling /tmp. That tends to be bad news...
                  – Stephen Kitt
                  Feb 13 '15 at 16:57














                up vote
                8
                down vote



                accepted










                I would use this, a simple one-liner:



                while ! ping -c1 HOSTNAME &>/dev/null; do echo "Ping Fail - `date`"; done ; echo "Host Found - `date`" ; /root/scripts/test1.sh


                Replace HOSTNAME with the host you are trying to ping.




                I missed the part about putting it in the background, put that line in a shellscript like so:



                #!/bin/sh

                while ! ping -c1 $1 &>/dev/null
                do echo "Ping Fail - `date`"
                done
                echo "Host Found - `date`"
                /root/scripts/test1.sh


                And to background it you would run it like so:



                nohup ./networktest.sh HOSTNAME > /tmp/networktest.out 2>&1 &


                Again replace HOSTNAME with the host you are trying to ping. In this approach you are passing the hostname as an argument to the shellscript.



                Just as a general warning, if your host stays down, you will have this script continuously pinging in the background until you either kill it or the host is found. So I would keep that in mind when you run this. Because you could end up eating system resources if you forget about this.






                share|improve this answer






















                • Tnx for the answer and the warning, this is a visualized environment so the script will start after the physical servers boot so the VM's will start certainly, but will keep in mind if i delete a VM, tnx again
                  – dusan90
                  Feb 12 '15 at 9:18











                • In particular by logging the failures every time, and storing all the output in a file in /tmp, if the host goes down you'll end up filling /tmp. That tends to be bad news...
                  – Stephen Kitt
                  Feb 13 '15 at 16:57












                up vote
                8
                down vote



                accepted







                up vote
                8
                down vote



                accepted






                I would use this, a simple one-liner:



                while ! ping -c1 HOSTNAME &>/dev/null; do echo "Ping Fail - `date`"; done ; echo "Host Found - `date`" ; /root/scripts/test1.sh


                Replace HOSTNAME with the host you are trying to ping.




                I missed the part about putting it in the background, put that line in a shellscript like so:



                #!/bin/sh

                while ! ping -c1 $1 &>/dev/null
                do echo "Ping Fail - `date`"
                done
                echo "Host Found - `date`"
                /root/scripts/test1.sh


                And to background it you would run it like so:



                nohup ./networktest.sh HOSTNAME > /tmp/networktest.out 2>&1 &


                Again replace HOSTNAME with the host you are trying to ping. In this approach you are passing the hostname as an argument to the shellscript.



                Just as a general warning, if your host stays down, you will have this script continuously pinging in the background until you either kill it or the host is found. So I would keep that in mind when you run this. Because you could end up eating system resources if you forget about this.






                share|improve this answer














                I would use this, a simple one-liner:



                while ! ping -c1 HOSTNAME &>/dev/null; do echo "Ping Fail - `date`"; done ; echo "Host Found - `date`" ; /root/scripts/test1.sh


                Replace HOSTNAME with the host you are trying to ping.




                I missed the part about putting it in the background, put that line in a shellscript like so:



                #!/bin/sh

                while ! ping -c1 $1 &>/dev/null
                do echo "Ping Fail - `date`"
                done
                echo "Host Found - `date`"
                /root/scripts/test1.sh


                And to background it you would run it like so:



                nohup ./networktest.sh HOSTNAME > /tmp/networktest.out 2>&1 &


                Again replace HOSTNAME with the host you are trying to ping. In this approach you are passing the hostname as an argument to the shellscript.



                Just as a general warning, if your host stays down, you will have this script continuously pinging in the background until you either kill it or the host is found. So I would keep that in mind when you run this. Because you could end up eating system resources if you forget about this.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 11 '15 at 16:24

























                answered Feb 11 '15 at 16:08









                devnull

                3,7991028




                3,7991028











                • Tnx for the answer and the warning, this is a visualized environment so the script will start after the physical servers boot so the VM's will start certainly, but will keep in mind if i delete a VM, tnx again
                  – dusan90
                  Feb 12 '15 at 9:18











                • In particular by logging the failures every time, and storing all the output in a file in /tmp, if the host goes down you'll end up filling /tmp. That tends to be bad news...
                  – Stephen Kitt
                  Feb 13 '15 at 16:57
















                • Tnx for the answer and the warning, this is a visualized environment so the script will start after the physical servers boot so the VM's will start certainly, but will keep in mind if i delete a VM, tnx again
                  – dusan90
                  Feb 12 '15 at 9:18











                • In particular by logging the failures every time, and storing all the output in a file in /tmp, if the host goes down you'll end up filling /tmp. That tends to be bad news...
                  – Stephen Kitt
                  Feb 13 '15 at 16:57















                Tnx for the answer and the warning, this is a visualized environment so the script will start after the physical servers boot so the VM's will start certainly, but will keep in mind if i delete a VM, tnx again
                – dusan90
                Feb 12 '15 at 9:18





                Tnx for the answer and the warning, this is a visualized environment so the script will start after the physical servers boot so the VM's will start certainly, but will keep in mind if i delete a VM, tnx again
                – dusan90
                Feb 12 '15 at 9:18













                In particular by logging the failures every time, and storing all the output in a file in /tmp, if the host goes down you'll end up filling /tmp. That tends to be bad news...
                – Stephen Kitt
                Feb 13 '15 at 16:57




                In particular by logging the failures every time, and storing all the output in a file in /tmp, if the host goes down you'll end up filling /tmp. That tends to be bad news...
                – Stephen Kitt
                Feb 13 '15 at 16:57












                up vote
                4
                down vote













                By passing the parameters '-c 30' to ping, it will try 30 ping and stop. It will check after if the command succeeds. I think it is best to do a loop that contains one ping and check if this ping succeed. Something like that:



                while true;
                do
                ping -c1 google.com
                if [ $? -eq 0 ]
                then
                /root/scripts/test1.sh
                exit 0
                fi
                done


                If by still running on the foreground, you mean it is still printing on the terminal, you can redirect stdin and stdout to /dev/null .
                Hope it helps






                share|improve this answer






















                • By unlocking the prompt so i could work on something else, tnx for help
                  – dusan90
                  Feb 12 '15 at 9:17














                up vote
                4
                down vote













                By passing the parameters '-c 30' to ping, it will try 30 ping and stop. It will check after if the command succeeds. I think it is best to do a loop that contains one ping and check if this ping succeed. Something like that:



                while true;
                do
                ping -c1 google.com
                if [ $? -eq 0 ]
                then
                /root/scripts/test1.sh
                exit 0
                fi
                done


                If by still running on the foreground, you mean it is still printing on the terminal, you can redirect stdin and stdout to /dev/null .
                Hope it helps






                share|improve this answer






















                • By unlocking the prompt so i could work on something else, tnx for help
                  – dusan90
                  Feb 12 '15 at 9:17












                up vote
                4
                down vote










                up vote
                4
                down vote









                By passing the parameters '-c 30' to ping, it will try 30 ping and stop. It will check after if the command succeeds. I think it is best to do a loop that contains one ping and check if this ping succeed. Something like that:



                while true;
                do
                ping -c1 google.com
                if [ $? -eq 0 ]
                then
                /root/scripts/test1.sh
                exit 0
                fi
                done


                If by still running on the foreground, you mean it is still printing on the terminal, you can redirect stdin and stdout to /dev/null .
                Hope it helps






                share|improve this answer














                By passing the parameters '-c 30' to ping, it will try 30 ping and stop. It will check after if the command succeeds. I think it is best to do a loop that contains one ping and check if this ping succeed. Something like that:



                while true;
                do
                ping -c1 google.com
                if [ $? -eq 0 ]
                then
                /root/scripts/test1.sh
                exit 0
                fi
                done


                If by still running on the foreground, you mean it is still printing on the terminal, you can redirect stdin and stdout to /dev/null .
                Hope it helps







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 11 '15 at 17:04









                muru

                34.3k579149




                34.3k579149










                answered Feb 11 '15 at 16:04









                David Bowman

                882




                882











                • By unlocking the prompt so i could work on something else, tnx for help
                  – dusan90
                  Feb 12 '15 at 9:17
















                • By unlocking the prompt so i could work on something else, tnx for help
                  – dusan90
                  Feb 12 '15 at 9:17















                By unlocking the prompt so i could work on something else, tnx for help
                – dusan90
                Feb 12 '15 at 9:17




                By unlocking the prompt so i could work on something else, tnx for help
                – dusan90
                Feb 12 '15 at 9:17










                up vote
                1
                down vote













                ping -oc 100000 Hostname > /dev/null && /root/scripts/test1.sh 



                • ping -o exits the ping after the first packet is received


                • > /dev/null redirects the output, so you won't see it


                • && would run the next command, if the previous command ere successful

                In addition, you can run any process in the background by adding & to the end of it; for example, echo "123" & will run in the background






                share|improve this answer
























                  up vote
                  1
                  down vote













                  ping -oc 100000 Hostname > /dev/null && /root/scripts/test1.sh 



                  • ping -o exits the ping after the first packet is received


                  • > /dev/null redirects the output, so you won't see it


                  • && would run the next command, if the previous command ere successful

                  In addition, you can run any process in the background by adding & to the end of it; for example, echo "123" & will run in the background






                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    ping -oc 100000 Hostname > /dev/null && /root/scripts/test1.sh 



                    • ping -o exits the ping after the first packet is received


                    • > /dev/null redirects the output, so you won't see it


                    • && would run the next command, if the previous command ere successful

                    In addition, you can run any process in the background by adding & to the end of it; for example, echo "123" & will run in the background






                    share|improve this answer












                    ping -oc 100000 Hostname > /dev/null && /root/scripts/test1.sh 



                    • ping -o exits the ping after the first packet is received


                    • > /dev/null redirects the output, so you won't see it


                    • && would run the next command, if the previous command ere successful

                    In addition, you can run any process in the background by adding & to the end of it; for example, echo "123" & will run in the background







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 11 '15 at 16:16









                    Aman

                    1485




                    1485




















                        up vote
                        0
                        down vote













                        An old post, but as a suggestion you can use the -w option on ping to avoid the loop. For example,



                        ping -w 30 -c 1 host


                        will try for 30 seconds with one ping per second (default ping has 1 second interval between pings) and will exit on the first successful ping.



                        If you don't need a timeout, I.e. wait for ever, just use a very large value with -w.






                        share|improve this answer


























                          up vote
                          0
                          down vote













                          An old post, but as a suggestion you can use the -w option on ping to avoid the loop. For example,



                          ping -w 30 -c 1 host


                          will try for 30 seconds with one ping per second (default ping has 1 second interval between pings) and will exit on the first successful ping.



                          If you don't need a timeout, I.e. wait for ever, just use a very large value with -w.






                          share|improve this answer
























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            An old post, but as a suggestion you can use the -w option on ping to avoid the loop. For example,



                            ping -w 30 -c 1 host


                            will try for 30 seconds with one ping per second (default ping has 1 second interval between pings) and will exit on the first successful ping.



                            If you don't need a timeout, I.e. wait for ever, just use a very large value with -w.






                            share|improve this answer














                            An old post, but as a suggestion you can use the -w option on ping to avoid the loop. For example,



                            ping -w 30 -c 1 host


                            will try for 30 seconds with one ping per second (default ping has 1 second interval between pings) and will exit on the first successful ping.



                            If you don't need a timeout, I.e. wait for ever, just use a very large value with -w.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Aug 24 '16 at 15:01









                            HalosGhost

                            3,62592035




                            3,62592035










                            answered Aug 24 '16 at 14:36









                            Chris Welch

                            1




                            1




















                                up vote
                                0
                                down vote













                                is there any script like, i need to ping from my Ubuntu Virtual Machine to Other Virtual Machine with time interval 30 minutes automatically. It will do 5 ping and stop ping my server then again ping command will automatically generate after 30 minutes.



                                Md Nazmus Sakib
                                IT Administrator
                                mail:nazmussakib021@gmail.com





                                share








                                New contributor




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





















                                  up vote
                                  0
                                  down vote













                                  is there any script like, i need to ping from my Ubuntu Virtual Machine to Other Virtual Machine with time interval 30 minutes automatically. It will do 5 ping and stop ping my server then again ping command will automatically generate after 30 minutes.



                                  Md Nazmus Sakib
                                  IT Administrator
                                  mail:nazmussakib021@gmail.com





                                  share








                                  New contributor




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



















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    is there any script like, i need to ping from my Ubuntu Virtual Machine to Other Virtual Machine with time interval 30 minutes automatically. It will do 5 ping and stop ping my server then again ping command will automatically generate after 30 minutes.



                                    Md Nazmus Sakib
                                    IT Administrator
                                    mail:nazmussakib021@gmail.com





                                    share








                                    New contributor




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









                                    is there any script like, i need to ping from my Ubuntu Virtual Machine to Other Virtual Machine with time interval 30 minutes automatically. It will do 5 ping and stop ping my server then again ping command will automatically generate after 30 minutes.



                                    Md Nazmus Sakib
                                    IT Administrator
                                    mail:nazmussakib021@gmail.com






                                    share








                                    New contributor




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








                                    share


                                    share






                                    New contributor




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









                                    answered 2 mins ago









                                    Md nazmus Sakib

                                    1




                                    1




                                    New contributor




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





                                    New contributor





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






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



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f184266%2fbash-ping-script-file-for-checking-host-availability%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Popular posts from this blog

                                        Peggy Mitchell

                                        Palaiologos

                                        The Forum (Inglewood, California)