if [ "$(ping -q -c1 google.com)" ];then wget -mnd -q http://www.google.com/intl/en_ALL/images/logo.gif ;fi

Use ping to test if a server is up

Bash scrip to test if a server is up, you can use this before wget'ing a file to make sure a blank one isn't downloaded.

-1
By: alf
2010-03-23 04:15:03

These Might Interest You

  • pings a server once per second, and beeps when the server is unreachable. Basically the opposite of: ping -a server-or-ip.com which would beep when a server IS reachable. You could also substitute beep with any command, which makes this a powerful alternative to ping -a: while true; do [ "$(ping -c1W1w1 server-or-ip.com 2>/dev/null | awk '/received/ {print $4}')" = 1 ] && date || echo 'server is down!'; sleep 1; done which would output the date and time every sec until the ping failed, in which case it would echo. Notes: Requires beep package. May need to run as root (beep uses the system speaker) Tested on Ubuntu which doesn't have beep out of the box... sudo apt-get install beep


    14
    while true; do [ "$(ping -c1W1w1 server-or-ip.com | awk '/received/ {print $4}')" != 1 ] && beep; sleep 1; done
    sudopeople · 2009-03-31 20:47:56 4
  • For some reason the 2&>1 does not work for me, but the shorter stdout/stderr redirection >& works perfectly (Ubuntu 10.04).


    2
    ping -q -c1 -w3 server.example.com >& /dev/null || echo server.example.com ping failed | mail -ne -s'Server unavailable' admin@example.com
    brainstorm · 2010-09-08 12:19:29 1
  • Joker wants an email if the Brand X server is down. Set a cron job for every 5 mins with this line and he gets an email when/if a ping takes longer than 3 seconds. Show Sample Output


    7
    ping -q -c1 -w3 brandx.jp.sme 2&>1 /dev/null || echo brandx.jp.sme ping failed | mail -ne -s'Server unavailable' joker@jp.co.uk
    mccalni · 2009-10-13 14:13:04 6
  • Waiting for your server to finish rebooting? Issue the command above and you will hear a beep when it comes online. The -i 60 flag tells ping to wait for 60 seconds between ping, putting less strain on your system. Vary it to your need. The -a flag tells ping to include an audible bell in the output when a package is received (that is, when your server comes online).


    110
    ping -i 60 -a IP_address
    haivu · 2009-03-04 06:21:22 7
  • This is like ping -a, but it does the opposite. It alerts you if the network is down, not up. Note that the beep will be from the speaker on the server, not from your terminal. Once a second, this script checks if the Internet is accessible and beeps if it is not. I define the Net as being "UP", if I can ping Google's public DNS server (8.8.8.8), but of course you could pick a different static IP address. I redirect the beep to /dev/console so that I can run this in the background from /etc/rc.local. Of course, doing that requires that the script is run by a UID or GID that has write permissions to /dev/console (usually only root). Question: I am not sure if the -W1 flag works under BSD. I have only tested this under GNU/Linux using ping from iputils. If anybody knows how portable -W is, please post a comment.


    1
    while :; do ping -W1 -c1 -n 8.8.8.8 > /dev/null || tput bel > /dev/console; sleep 1; done
    hackerb9 · 2010-09-24 06:34:12 1
  • Quick and dirty one-liner to get the average ping(1) time from a server. Show Sample Output


    -4
    ping -qc 10 server.tld | awk -F/ '/^rtt/ {print $5}'
    atoponce · 2011-10-12 21:07:06 2

What Others Think

If you're not going to have an "else" clause, you can shorten that to: [ "$(ping -q -c1 google.com)" ] && wget ... 'if' works by using the '[' (test) command, but you can use it directly.
flatcap · 426 weeks and 1 day ago
Alternatively (not as pretty): ping -c1 google.com > /dev/null && wget ...
flatcap · 426 weeks and 1 day ago
Wouldn't it be better to use curl -I to check the headers for a 200 OK? Just because a machine is responding to pings, that doesn't mean that the webserver is up.
eightmillion · 426 weeks ago
I don't have the means to test this example at the moment, but I believe it should work. curl -Is google.com 2>&1 | grep -q "200 OK" && wget ...
eightmillion · 426 weeks ago
Great alternatives, I'll have to give these a try!
alf · 426 weeks ago
'if' expects a command. '[' is actually a program, not part of the shell. So you can actually just do if ping -q -c1 google.com; then ...
recursiverse · 426 weeks ago
No need for the 'then' or even an 'else', just use && or || e.g.: ping -c1 host.com > /dev/null && echo "YES" || echo "NO" This is the equivalent of: if ping -c1 host .com > /dev/null then echo "YES" else echo "NO" fi
finkployd · 397 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: