I have data loggers spread around the solar power system, and a single program that runs at midnight to collect the day's data and put it on a hard drive.

The bash script on the Ubuntu server steps through 7 Raspbian units that have the data collected and puts it in the proper place on the hard drive, in directories named by the units out there. I figured out how to take the generic names today.dat and today.png and save them with the day's date in the archive like 2017-08-10.png and 2017-08-10.dat

--> Occasionally one of the units will not respond due to some problem or other, and it blocks execution of the program.

So I would like to use ping to test whether the system responds before I decide whether to try to gather data from it.

My system names are all in the /etc/hosts file so I reference them by name.

For purposes of this question I am checking SOLAR - the one that monitors the charge controller and inverter behavior.

Here is what I have tried:

if $( ping -c 1 SOLAR | grep icmp* | wc -l ) eq 0
   (do stuff like ssh -e "gnuplot makepng" then scp the png and raw data)

(too complicated)


if [ $(ping -c 1 SOLAR >/dev/null) eq 0 ]
    then do the stuff

(looks OK but I don't understand the need for the square brackets)

There must be a better way.

I am hoping to do something more like

if $(ping -c 1 SOLAR) eq 0
   (do stuff)
   (then maybe echo "SOLAR didn't respond" | ssmtp myname@gmail.com

I haven't programmed bash in a couple of decades - used C shell most of the time. And I am getting way too much varying advice all over the net about the best way to do this.

So can anybody please suggest a simple way to do this?

  • You know to write foo eq 0, but don't know why [ ] is needed or that it's -eq? You need a bash tutorial. – muru Aug 11 '17 at 5:24
  • What can I say? I'm flattered you took the time to go through my 2500+ answers on SE to conclude that I couldn't write such an answer. – muru Aug 12 '17 at 14:12
  • Good point. Shifting from C Shell to bash (when I used to use korn long ago) is requiring some tutoring. And yes I have seen a lot of examples where you do so quite thoroughly, @muru, so I take that back. --> In any case, while you were weighing in about my lack of ability (the reason I had to ask the question), the answer was simultaneously being written and within a couple of hours was incorporated into my program, where it will now save me time every day. – SDsolar Aug 12 '17 at 23:15
  • it wasn't the lack of ability so much as the danger - say you wrote something that looks it works, but that was actually due to a quirk of syntax, and you deploy it somewhere where it can cause actual damage due to some edge case you couldn't test... – muru Aug 13 '17 at 0:06

It seems you are only interested in the exit status of ping i.e. whether you are getting ICMP ECHO_RESPONSE from the host to your ECHO_REQUEST; you can just do ping -c 1 ... and ping would exit with status 0 if the host is sending response, 1 if no response and 2 for unknown host.

You can easily use this with if construct; you don't need to count response, just use ping as if's condition as if would evaluate the exit status of ping to proceed to any defined branch:

if ping -c 1 SOLAR &>/dev/null; then
    ## Reachable -- do stuffs
    ## Unreachable -- do other stuffs

I am redirecting ping's STDOUT and STDERR streams to /dev/null as we are not interested in those.

| improve this answer | |
  • So the if statement will evaluate success or failure and I don't need to check the numerical return value, eh? This is short and sweet - just what I have been looking for. Thank you. – SDsolar Aug 11 '17 at 5:34
  • 1
    @SDsolar Read it as if true (exit status is not like the return value BTW). $() is called command substitution, it has specific use cases but not needed here. – heemayl Aug 11 '17 at 5:35
  • Thank you again, @heemayl. You have helped me a lot. I was having the midnight program email me a log file and needed to look at it carefully each day to catch anomalies. Now I get email alerts regarding only those sensors in need of a touch of TLC. That's a HUGE improvement. TNX AGN. – SDsolar Aug 12 '17 at 3:29
  • 1
    @SDsolar Great. Glad I could help :) – heemayl Aug 12 '17 at 8:02

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.