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 ] 
  sudo /sbin/shutdown -r now

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:

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 ----
echo ----
echo RESTART # pi restarts since there's no connection
echo ----

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

  • Sorry about that -- of course you are using a 3G dongle so that's not very possible unless you do have a router/LAN and can connect a second interface to test that way.
    – goldilocks
    Commented May 11, 2016 at 13:23
  • Instead of ping, can you try wget or ssh to another host? It should timeout after some time on error. And instead of a reboot try ifdown/ifup of the interface.
    – ott--
    Commented May 11, 2016 at 17:37
  • 1
    Probably this won't solve your problem but I'd suggest you to send more than 4 packages to test connectivity, you will avoid some false positives. In addition, consider to ping directly a IP address ping because, in case that the DNS you're using don't work, you probably can SSH, but ping google.com won't work (in case you use google DNS ( or that won't be necessary)
    – migrc
    Commented May 13, 2016 at 11:37
  • Could it be that Weaved itself has got into a state where it cannot connect (or has even quit) even though Google may remain pingable? I had a similar issue with ngrok. Could you use something like monit or supervisor to monitor and restart that process?
    – milloy
    Commented Jul 12, 2016 at 9:44

3 Answers 3


I was facing a very similar situation. I ended up not using ping but curl. because many times, ping will still work even though the mobile 3G/4G connection has run out of credits/data -- while accessing the web is blocked. On some networks, ping does never work (because ICMP protocol is blocked).

In any case, ping is not a reliable way for testing internet connectivity.

This is how I check whether the 3G/4G connection has still access to the internet:

  1. curl http://checkip.amazonaws.com/ the result should be your mobile IP address. You can verify the result, i.e. with php filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
  2. curl http://yourwebsite.com/some-keyword save any keyword on any website that you have access to and just curl its URL. Compare the result with your expected keyword.

If your mobile 3G/4G connection expired, the data volume is exhausted, or the prepaid credits have run out, the curl result will be something entirely different or not work at all. By testing for an expected value, you can make sure whether your mobile connection is still able to access http websites.

You could combine my method with timeout or just use a curl options to control timeouts.

This method is working with approx. 15 different prepaid mobile carriers world-wide.

Update: You might want to use curl --head (Show document info only), this will not download the http response body. It will save you a few bytes on traffic and is a bit faster. Check the response header for HTTP/1.1 200 OK.

I mostly use that as a "ping" to measure how fast http response is. It's not accurate, since your mobile provider might inject his Please refill your prepaid account to keep surfing with 4G speed message into the response body. Most of the time however, they are redirecting you to their own site and you'd get a HTTP/1.1 302 response.

  • thank you very much... this is an excellent way. I ended solving the problem with a logging script that checks if the original script (send data via mail) succeeded. If that wasn't the case the pi restarted. This setup worked pretty good but your approach is way better. I'm posting my solution in my original question.
    – Peter S
    Commented Aug 21, 2017 at 10:53

I've tried blocking my Pi's traffic as a test case. It seems that ping waits a little too long when the connection is frozen. I could be wrong though.

Anyways, try adding a timeout to your script.

# 10 second timeout. Will quit "ping" after 10 seconds and set $? to error code 124.
timeout 10 ping -c4 google.com > /dev/null 

if [ $? != 0 ] 
  sudo /sbin/shutdown -r now
  • I can't check it right now, but I think there is a parameter of the ping command to limit the reply waiting time.
    – migrc
    Commented May 13, 2016 at 11:23
  • @migrc I'm aware of that. It's the -W and -w for timeout and deadline respectively. I did try them and they don't alleviate the problem. I also tried adding interval switch -i into the mix and it did not help.
    – Aloha
    Commented May 13, 2016 at 11:50

Not an answer to the original question, but relevant to the problem. There is no 3G dongle in the world that requires only 100mA to work properly. These devices need more than 0.5W. Show us the datasheet. What would happen if your dongle didn't get enough current? It would freeze or otherwise malfunction in obscure ways. The Pi3 can source more current on a USB port, otherwise the solution will be a USB Y-cable. Try sticking the dongle into a linux laptop, connect as you would on your Pi2, , and issue lsusb -v. What does it say?

  • Hi. Thanks for your suggestion but I don't think that the power supply presents a problem since I've been using the setup for over a year now. I've been using my own approach on how to handle the Online status of the pi via logging the success of the original script into a file and checking it regularly --> if there's no successful log the pi restarts and the original script is started again.
    – Peter S
    Commented Aug 21, 2017 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.