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...
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...
A further simplification of Martynas' answer:
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:
It's a bit circuitous, but if you want the loop to be cancellable it should do the trick.
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 beforesshd
is started.)You can do a loop, send one ping and depending on the status break the loop, for example (bash):
Putting this somewhere in your script will block, until
www.google.com
is pingable.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.
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.
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.
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.
also useful for testing if machines are up before doing something on them. A simple example:
For macOS users,
ping
has the option-o
specifically for this:So the command is simple:
It returns 0 when the host has been successfully pinged once.
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.
Folks, yes this is old. A lot of good answers here.
But I just found what I call a beauty! In case someone likes native
ping
counter:ping $HOST | sed "/ ms$/ q"
Isn't this fascinating? :)