beep when a server goes offline

while true; do [ "$(ping -c1W1w1 server-or-ip.com | awk '/received/ {print $4}')" != 1 ] && beep; sleep 1; done
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

These Might Interest You

  • 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
  • Are there any creative pieces of music that can be created using beep and the shell? I'd love to hear it!


    5
    man beep | sed -e '1,/Note/d; /BUGS/,$d' | awk '{print $2}' | xargs -IX sudo beep -f X -l 500
    jnash · 2009-04-01 06:48:48 4
  • PING parameters c 1 limits to 1 pinging attempt q makes the command quiet (or silent mode) /dev/null 2>&1 is to remove the display && echo ONLINE is executed if previous command is successful (return value 0) || echo OFFLINE is executed otherwise (return value of 1 if unreachable or 2 if you're offline yourself). I personally use this command as an alias with a predefined machine name but there are at least 2 improvements that may be done. Asking for the machine name or IP Escaping the output so that it displays ONLINE in green and OFFLINE in red (for instance).


    9
    ping -c 1 -q MACHINE_IP_OR_NAME >/dev/null 2>&1 && echo ONLINE || echo OFFLINE
    UnixNeko · 2012-02-09 06:30:55 7
  • or change the frequency of the beep


    0
    setterm -bfreq 0
    mvrilo · 2009-11-16 17:34:39 0

What Others Think

I think you can get rid of a bit of complexity (and the awk) out of this by simply using the bash OR operator, instead of the contitional test used now. Also, by using the bash block operators, { }, you can jam multiple commands after the OR expressions. while true; do ping -c1W1w1 server-or-ip.com 2>/dev/null || { echo Server down; beep; } ; sleep 1; done And another slight edit to do branching: while true; do ping -c1W1w1 google.com &>/dev/null && { echo Up;} || { echo Down; beep; } ; sleep 1; done Runs one block if the server is up, and another if it's down. Hope this helps!
clockworkavian · 480 weeks and 6 days ago
@clockworkavian: thanks for reminding me about the braces :) and the optomizations make this much easier to read, ty ty!
sudopeople · 480 weeks and 5 days ago
Great command, nice. I am a mac user so, beep isn't installed and the ping command has no -w option. I played around and made this mac version. It is just a small modification to clockworkavian's way with using the bash OR operator, cleaning up ping and using the mac speech synthesis to alert you what is going on. while true; do ping -c1 -W1 google.com &>/dev/null && { say "up";} || { say "down"; } ; sleep 1; done if the "up" goes on your nerves after sometime you could just take it out.
netsaint · 458 weeks and 4 days ago
Mac users can just use the -A option for ping which will beep when no reply is received. you can also use -a if you want an "Everything is OK" alarm.
LIV2 · 406 weeks and 4 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: