1

My Headless PI has been running for months without any problems, but in the last few days I've run into problems SSH'ing to it, and thus have had to pull the power.

  • I had disabled powersavig mode in the interfaces file (which helped loads!)
  • I run a batch script frequently "/usr/src/scripts/wifi_test_2.sh" using cron to reconnect the WIFI if it has lost the connection.
  • I reboot the PI every day at 1 PM.

I've been looking in the syslog file to try and find out why this may be happening but i'm getting myself confused, so I've switched my attention to a patch, not a fix.

From what I understand of the logs, the wifi seems to stop trying to re connect and will only reestablish following a reboot.

A nasty but effective way of re establishing the connection, is to sudo shutdown -r now but I'm running a headless PI so I have to pull the power.

Could someone give me some pointers on how to modify this script to force a reboot if the script has been called 10 times in succession, but the wifi is still down.

Script

#!/bin/bash                                  

LOGFILE=/var/log/wifitestlogfile.log 

TESTIP=74.125.224.72                          

ping -c4 ${TESTIP} > /dev/null 

if [ $? != 0 ]                            
then

        message="$(date) -- WiFi seems down, restarting - message from script  /usr/src/scripts/wifi_test_2.sh"
        # echo >$LOGFILE  empties the file so just the last log is saved
        echo $message >>$LOGFILE

    ifdown --force wlan0                     
    ifup wlan0                               
else       
        message="$(date) -- WiFi seems up - message from script  /usr/src/scripts/wifi_test_2.sh"
        # echo >$LOGFILE  empties the file so just the last log is saved
        echo $message >>$LOGFILE


fi   

from wifitestlogfile.log

Mon Sep 15 16:20:14 BST 2014 -- WiFi seems up - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:21:05 BST 2014 -- WiFi seems up - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:22:05 BST 2014 -- WiFi seems up - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:23:05 BST 2014 -- WiFi seems up - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:24:04 BST 2014 -- WiFi seems up - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:25:05 BST 2014 -- WiFi seems up - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:26:05 BST 2014 -- WiFi seems up - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:27:02 BST 2014 -- WiFi seems down, restarting - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:28:01 BST 2014 -- WiFi seems down, restarting - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:29:02 BST 2014 -- WiFi seems down, restarting - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:30:01 BST 2014 -- WiFi seems down, restarting - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:31:01 BST 2014 -- WiFi seems down, restarting - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:32:02 BST 2014 -- WiFi seems down, restarting - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:33:01 BST 2014 -- WiFi seems down, restarting - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:34:01 BST 2014 -- WiFi seems down, restarting - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:35:01 BST 2014 -- WiFi seems down, restarting - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:36:01 BST 2014 -- WiFi seems down, restarting - message from script /usr/src/scripts/wifi_test_2.sh
Mon Sep 15 16:37:01 BST 2014 -- WiFi seems down, restarting - message from script /usr/src/scripts/wifi_test_2.sh


**at this point force a reboot**
2

Create a global (research export) shell variable or a file (for example in /var) that holds the number of times the script tried to restart wifi. Increment on every try. Add a check to see if it has reached a preset number. If it has, call reboot now. On a successful connection, reset the value of the variable (or file).

Good luck!

1

I may be seeking the wrath of others, this would probably be better as a comment but I don't have the reputation for that. I copied your example log file and script and it seemed to work fine. I'm not sure what the issue is you're having?

The one change you can make to the script that may help is to add the -x parameter to the first line to help you see the processing that's going on.

#!/bin/bash -x
  • sorry, i should have answered my question before, when i got it working! – reggie Sep 18 '14 at 21:25
0

After further work, this seems to work for me:

#!/bin/bash                                  

LOGFILE=/var/log/wifitestlogfile.log 

TESTIP=74.125.224.72                          

ping -c4 ${TESTIP} > /dev/null 

if [ $? != 0 ]                            
then

        message="$(date) -- WiFi seems down, restarting - message from script  /usr/src/scripts/wifi_test_2.sh"
        # echo >$LOGFILE  empties the file so just the last log is saved
        echo $message >>$LOGFILE

        ifdown --force wlan0                     
        ifup wlan0                               

        downcount=$(grep down /var/log/wifitestlogfile.log | wc -l) # counts the times the letters "down" are in the logfile and passes it to the variable doencount
        echo ${downcount} >>$LOGFILE

        if ((${downcount} >= 60 )); then

                    echo >$LOGFILE # empties the file
                   sudo shutdown -r now 

        fi





else       
        message="$(date) -- WiFi seems up - message from script  /usr/src/scripts/wifi_test_2.sh"
        # echo >$LOGFILE  empties the file so just the last log is saved
        echo $message >>$LOGFILE

fi

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.