Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Detecting curl return error and wait in loop?

Sat Jan 12, 2019 10:40 am

I have a report script which runs on boot in order to send a message to myself when the RPi starts up containing the current IP address of the RPi.
The script was created in order to be able to move the RPi between networks and after startup find its address easily to use when connecting with PuTTY over ssh.

Now I am facing a problem that happens when the reason for the reboot is that a power outage has happened and the router to which the Pi is connected also restarts. In order for the address message to actually be sent the RPi needs to wait for the router to come on-line, i.e. it has a connection to the Internet.
And this is what I need to figure out...

So here is the script I am running on startup (with a 60 second delay to allow for the RPi networking to stabilize):

Code: Select all

#!/bin/bash
# ---- Script used to report the RPi IP address using the script ***********.com/php/reportip.php -----

#Wait for a minute (at boot time) if no argument supplied, otherwise use supplied delay:
if [ -z "$1" ]; then
  DELAY="60"
else
  DELAY="$1"
fi
sleep $DELAY

RPIname=$(hostname)
IPAddr=$(ifconfig eth0|grep "inet addr"|sed 's/ *inet addr://;s/ .*//')
IPAddrWiFi=$(ifconfig wlan0|grep "inet addr"|sed 's/ *inet addr://;s/ .*//')
IPMac=$(cat /sys/class/net/eth0/address)
IPMacWiFi=$(cat /sys/class/net/wlan0/address)

echo "Hostname:    $RPIname"
echo "Eth IP addr: $IPAddr"
echo "Eth  MAC:    $IPMac"
echo "WiFi addr:   $IPAddrWiFi"
echo "WiFi MAC:    $IPMacWiFi"

#Send request
curl --data "rpiaddr=$IPAddr" --data "rpiname=$RPIname" --data "rpiaddrwifi=$IPAddrWiFi" --data "rpimacaddr=$IPMac"  --data "rpimacaddrwifi=$IPMacWiFi" http://***********.com/php/ipreport.php
exit 0
What I need to figure out is the best approach to handle a power outage return:
1- If the RPi gets neither an eth0 nor a wlan0 address it should wait a minute and then repeat from the start
2- If the Rpi has a valid IP address (on eth0 or wlan0) then it shall run the curl command, but if that returns with an error the script should wait a minute and then loop back and retry the curl command.

Obviously I am not very versed in bash script writing, I had some problems coming up to the above script a few years back when I created the system...

So how can I check something like this and if this something happens do a delay using sleep(60) and then retry the command that failed?
I.e. how can I jump back?

What will be the return on the curl call if the server is unreachable?
And how does one extract the return of a curl command?

Grateful for all suggestions.

Right now my remote RPi is off-line due to a power outage at the location and I want to upgrade my script as soon as it comes back on line.
Since it is set with a cron job to restart at 01:15 I will get a message then if things have stabilized, but I would rather not wait until then for future power outages...
Bo Berglund
Sweden

Return to “Raspberry Pi OS”