My Pi 2 is uses a USB Modem 3G connection.
Sometimes the connection gets stuck and the Pi is no longer reachable via SSH, (Weaved).
The Pi itself looks normal, ( the Pi is on and the light on USB modem indicates that the Pi is connected)
I tried using a bash script to evaluate the connection and restart the Pi, if there's no connection.
ping -c4 google.com > /dev/null
if [ $? != 0 ]
then
sudo /sbin/shutdown -r now
fi
Cron starts the script every 5 Minutes.
The script works, if I unplug the USB Modem but doesn't when the connection freezes and the Pi gets unreachable via SSH. It looks like the Pi is still able to ping.
Since my 3G provider works on a prepaid sim card, I even tried the setup without any credit on the sim card but the Pi was still able to ping google.com and did not restart although everything else did not work --> when logged into my Weaved account the Pi was always displayed as offline.
So my question would be how I could solve the problem so that my Pi is always reachable via Weaved and other internet sites. (The Pi checks a mail account every minute and performs tasks based on the messages).
Would watchdog work for this purpose?
I looked it up and found Do network interfaces receive traffic? as one of the listed tasks on the watchdog website.
Update: Thanks for the comments/edits and answers so far.
@PandaLion98: I tried your version but nothing changed. The Pi still does not restart when USB Dongle is connected but Pi is not visible as online.
@ott: I am not sure if this is a usable solution, or if I get what you mean. I don't have another host available to connect via ssh from the pi.
Eugen provided me with a nice approach to this problem via curl: I tried it on my Pi and it works--> here's the script i use:
#!/bin/bash
curl --head -o /Users/LinkToFile/curl_test.txt google.com
declare -i Count=$(grep -c "private" "/Users/LinkToFile/curl_test.txt")
if (( $Count > 0 )); then
echo ----
echo OK # so the script shuts down since there's a connection
echo ----
else
echo ----
echo RESTART # pi restarts since there's no connection
echo ----
fi
the curl_test.txt looks like this:
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Location: http://www.google.com/................
Content-Length: 256
Date: Mon, 21 Aug 2017 10:52:28 GMT
So I am looking for the word privatein the txt file. If it's there, everything is ok. If not --> restart
in the end of the script curl_test.txtis deleted