How can I ping a certain address and when found, stop pinging.

I want to use it in a bash script, so when the host is starting up, the script keeps on pinging and from the moment the host is available, the script continues...

16 Answers 16


A further simplification of Martynas' answer:

until ping -c1 www.google.com >/dev/null 2>&1; do :; done

note that ping itself is used as the loop test; as soon as it succeeds, the loop ends. The loop body is empty, with the null command ":" used to prevent a syntax error.

Update: I thought of a way to make Control-C exit the ping loop cleanly. This will run the loop in the background, trap the interrupt (Control-C) signal, and kill the background loop if it occurs:

ping_cancelled=false    # Keep track of whether the loop was cancelled, or succeeded
until ping -c1 "$1" >/dev/null 2>&1; do :; done &    # The "&" backgrounds it
trap "kill $!; ping_cancelled=true" SIGINT
wait $!          # Wait for the loop to exit, one way or another
trap - SIGINT    # Remove the trap, now we're done with it
echo "Done pinging, cancelled=$ping_cancelled"

It's a bit circuitous, but if you want the loop to be cancellable it should do the trick.

| improve this answer | |
  • 6
    To add 'sleep' (e.g. each 5 secs): while ! ping -c1 www.google.com &>/dev/null; do sleep 5; done – lepe Apr 11 '15 at 3:55
  • 4
    By default ping will wait 10 seconds before giving up on its ping. If you want to reduce that to 1 second, you can use -w1. – Jack O'Connor Dec 1 '16 at 19:44
  • 2
    @JackO'Connor Using BSD ping, this is a capital -W1 – Luke Exton Mar 31 '17 at 19:17
  • My only issue with this answer is it doesn't handle SIGINT well. You are unable to stop this from the command line, if it is never going to connect for whatever reason. – Luke Exton Mar 31 '17 at 19:18
  • 1
    @LukeExton That's due to the ways bash and ping handle SIGINT, and how they interact (see this question). I don't have a very good solution, but there's a kluge: use Control-Z to stop the script, then kill %1 to kill it. – Gordon Davisson Mar 31 '17 at 21:47

I know the question is old... and specifically asks regarding ping, but I wanted to share my solution.

I use this when rebooting hosts to know when I can SSH back into them again. (Since ping will respond for several seconds before sshd is started.)

until nc -vzw 2 $host 22; do sleep 2; done
| improve this answer | |
  • 2
    That's exactly what I searched for, thanks! You can even interrupt the call with CTRL+C which is not the case for some other solutions in this thread. – Alex Jan 19 '14 at 8:58
  • 1
    Note this works for BSD versions of netcat. The nmap.org version in Linux distros is implemented differently, so the -z option doesn't exist, and the -w option waits for a connection but doesn't close when successful. Works great in OSX, though. – Paul Jan 8 '16 at 16:59
  • This is why you don't stop at the first (fine) answer. This is the answer to the real problem. – Leo Nov 3 '19 at 9:36
  • 1
    Regarding whether this works on "Linux distros" or not, the default version of netcat in Ubuntu 18.04 is of the BSD variety and works exactly as described here. – spikyjt Feb 25 at 13:47

You can do a loop, send one ping and depending on the status break the loop, for example (bash):

while true; do ping -c1 www.google.com > /dev/null && break; done

Putting this somewhere in your script will block, until www.google.com is pingable.

| improve this answer | |
  • 2
    This while true and break is the cleaner solution, IMO. – Dan Carley Jul 17 '09 at 8:26
  • 1
    @DanCarley I'm going to disagree with you for the reason that in the accepted answer, the null command version, one could substitute a sleep/wait to give your CPU a break, and in which case I would call it the cleaner solution. The other problem with this answer, is that it requires the reader/user to understand how the && works, and how that if the first command fails, it won't be called. This is just an opinion, like your own. – Hamid Jan 11 '15 at 19:06
  • 1
    The main difference I see between this solution and the accepted one, is that this one will echo the error message "unknown host ..." until its found. To add sleep (e.g. each 5 secs): while true; do sleep 5; ping ... – lepe Apr 10 '15 at 2:23

Ping the target host once. Check if the ping succeeded (return value of ping is zero). If host is not alive, ping again.

The following code can be saved as a file and called with the hostname as argument, or stripped of the first and last line and used as function within an existing script (waitForHost hostname).

The code does not evaluate the cause for failure if the ping does not result in a response, thus looping forever if the host does not exist. My BSD manpage lists the meaning of each return value, while the linux one does not, so I guess this might not be portable, that's why I left it out.


PING=`which ping`

function waitForHost
    if [ -n "$1" ]; 
        waitForHost1 $1;
        echo "waitForHost: Hostname argument expected"

function waitForHost1
    while [ $reachable -eq 0 ];
    $PING -q -c 1 $1
    if [ "$?" -eq 0 ];
    sleep 5
waitForHost $1
| improve this answer | |
while [ $UNREACHEABLE -ne "0" ]; 
   do ping -q -c 1 HOST &> /dev/null; UNREACHEABLE=$?; sleep 1;

You may remove sleep 1, it's only here to prevent any flooding problem in case where the host would be reacheable but ping would not exit with code 0.

| improve this answer | |

Please see good options at stackoverflow. Here is a sample in bash, you will have to loop over the following code until it returns a successfull ping result.

ping -c 1 -t 1;
if [ $? -eq 0 ]; then
    echo " is up";
    echo "ip is down";

| improve this answer | |

any of the above loops can also be used with fping rather than ping which, IMO, is better suited for use in scripts than ping itself. See fping(1) for details.

while ! fping -q $HOSTNAMES ; do :; done

also useful for testing if machines are up before doing something on them. A simple example:

for h in HOST1 HOST2 HOST3 ; do
  if fping -q $h ; then
     echo -n "$h : "
     ssh $h "uname -a"
| improve this answer | |

This will try a given number of times.

t=4; c=0; r=0; until ping -c 1 hostname.com >/dev/null 2>&1 || ((++c >= t)); do r=$?; done; echo $r

Instead of echoing $r, you can test it and act according to its value:

if ((r)); then echo 'Failed to contact host'; else echo 'Continuing with script'; fi
| improve this answer | |

To nicely handle the SIGINT on BSD ping.

HOST=google.com NO=1; while [ $NO -ne 0 ]; do ping -W1 -c1 $HOST &>/dev/null; NO=$?;echo "$(date) ($HOST) $NO" ; done; echo "$(date) ($HOST) reachable"

as a function

  local NO=1
  while [ $NO -ne 0 ]; do
    ping -W1 -c1 $1 &>/dev/null; NO=$?
    # Optionally block ICMP flooding
    # sleep 1
    echo "$(date) ($1) ($NO)"
| improve this answer | |

For macOS users, ping has the option -o specifically for this:

-o      Exit successfully after receiving one reply packet.

So the command is simple:

ping -o www.google.com

It returns 0 when the host has been successfully pinged once.

| improve this answer | |

I've been using the following function. I like it because I can tell it to stop trying after a while:

#!/usr/bin/env bash

function networkup {
  # Initialize number of attempts
  while [ $reachable -ne 0 ]; do
    # Ping supplied host
    ping -q -c 1 -W 1 "$2" > /dev/null 2>&1
    # Check return code
    if [ $? -eq 0 ]; then
      # Success, we can exit with the right return code
      echo 0
    # Network down, decrement counter and try again
    let reachable-=1
    # Sleep for one second
    sleep 1
  # Network down, number of attempts exhausted, quiting
  echo 1

It can be used like this to launch something:

# Start-up a web browser, if network is up
if [ $(networkup 60 www.google.com) -eq 0 ]; then
  firefox &
| improve this answer | |

In general I want to wait for my database or other server to come up, but I don't want to wait too long. The following code waits for 10 seconds, then sets the exit code if the server did not appear within the time limit.

If the server appears before the time limit, the loop will be short-circuited so the next bit of code can run.

for i in `seq 1 10`; do date ; sleep 1 ; ping -c1 ${HOST} &>/dev/null && break ; done
| improve this answer | |

A further enhancement to Gordon Davisson's answer:

until $(ping -c1 www.google.com &>/dev/null); do :; done

with the surrounding '$()' a subshell is started and therefore you can use Control-C to terminate the loop in the event that the host does not become available.

| improve this answer | |
$ cat bin/check_host_up.sh
PINGCMD="ping -c1 $HOST >/dev/null 2>&1"
while true; do
    eval $PINGCMD && sleep 1 && echo -n . ||    \
    (                                           \
        echo && echo "(${HOST}) down:" $(date) &&         \
            until eval $PINGCMD; do :; done &&  \
        echo "(${HOST}) up  :" $(date)
| improve this answer | |

ping $HOSTNAME | grep from -m 1

| improve this answer | |

grep works on lines, so:

ping google.com | grep "not"

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.