Check a server is up. If it isn't mail me.

ping -q -c1 -w3 2&>1 /dev/null || echo ping failed | mail -ne -s'Server unavailable'
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.
Sample Output
An email saying " ping failed"

By: mccalni
2009-10-13 14:13:04

3 Alternatives + Submit Alt

What Others Think

seem not correct. When i execute "ping -q -c1 -w3 i got exit status 0 and when executing "ping -q -c1 -w3 2&>1 /dev/null" i got 1 ....
zhuzhixin · 626 weeks and 6 days ago
zhuzhixin, what distro you using? This using bash on Jaunty. From the ping man page ... ping is part of iputils package and the latest versions are available in source form at ... -q Quiet output.... -c count Stop after sending count ECHO_REQUEST packets. With deadline option, ping waits for count ECHO_REPLY packets, until the timeout expires. -w deadline Specify a timeout, in seconds, before ping exits regardless of how many packets have been sent or received....
mccalni · 626 weeks ago
I think maybe your firewall eats ping replies? Try an internal machine address and/or something you know you get ping replies from. My corporate firewall eats pings from google, yahoo, et al, but at home I can ping them fine.
mccalni · 625 weeks and 5 days ago
I don't think the part "2&>1 > /dev/null" does what you think it does. It's 2>&1, not 2&>1, and the 2>&1 should follow the >somefile part. It says send stderr to where I just sent stdout. Or you could have used >&. See these examples: >a ll a b 1 ls: cannot access b: No such file or directory ls: cannot access 1: No such file or directory -rw-r--r-- 1 splante 20000 0 Apr 7 18:08 a # your way just creates a file named 1 with stderr ll a b 2&>1 >/dev/null cat 1 ls: cannot access b: No such file or directory ls: cannot access 2: No such file or directory rm 1 # this doesn't send stderr to /dev/null ll a b 2>&1 >/dev/null ls: cannot access b: No such file or directory ll a b 1 ls: cannot access b: No such file or directory ls: cannot access 1: No such file or directory -rw-r--r-- 1 splante 20000 0 Apr 7 18:08 a # both of these work as expected ll a b >/dev/null 2>&1 ll a b >& /dev/null
splante · 549 weeks and 6 days ago
Man, thanks a lot, but my ping don't understand standarts stderr. I was pinging and (it's unroutable) and all outputs are same. What's hapen's guys? I using AltLinux 4.01.
Arkey · 545 weeks ago
I use modifide script: while true; do /bin/ping -q -c1 -w3 2&>1 /dev/null || echo " ping failed at $(date +%D) $(date +%H:%M:%S)" >> /var/log/ping.log; sleep 10; done & both 2&>1 and 2>&1 are same for me
Arkey · 545 weeks ago
Thank you for sharing, it helped me. I also wanted to share an interesting article about top worldwide branding companies on I hope that this information will be useful to someone. All the best and take care of your health in this difficult time.
klarkwhite · 11 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? 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.


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: