I'm trying to understand how this Python script works, but am struggling with 1 to 3 below, so if someone could explain these I'd appreciate it.

1) 'ping -c 2 -w 1 -q |grep "1 received" > /dev/null 2> /dev/null' Iv'e tried this at terminal, but it didn't work, how can I test it outside of this script?

2) 'logger "WLAN Down, Pi is forcing a reboot"' Where does this message get logged; Iv'e looked in syslog and messages, but cannot see the text inside the double quotes.

3) 'sudo /sbin/ifdown wlan0 && sleep 10 && sudo /sbin/ifup --force wlan0'

  import subprocess
    WLAN_check_flg = False

    def WLAN_check():
        This function checks if the WLAN is still up by pinging the router.
        If there is no return, we'll reset the WLAN connection.
        If the resetting of the WLAN does not work, we need to reset the Pi.
        source http://www.raspberrypi.org/forums/viewtopic.php?t=54001&p=413095

        ping_ret = subprocess.call(['ping -c 2 -w 1 -q |grep "1 received" > /dev/null 2> /dev/null'], shell=True)
        if ping_ret:
            # we lost the WLAN connection.
            # did we try a recovery already?
            if WLAN_check_flg:
                # we have a serious problem and need to reboot the Pi to recover the WLAN connection
                subprocess.call(['logger "WLAN Down, Pi is forcing a reboot"'], shell=True)
                WLAN_check_flg = False
                subprocess.call(['sudo shutdown -r now'], shell=True)
                # try to recover the connection by resetting the LAN
                subprocess.call(['logger "WLAN is down, Pi is resetting WLAN connection"'], shell=True)
                WLAN_check_flg = True # try to recover
                subprocess.call(['sudo /sbin/ifdown wlan0 && sleep 10 && sudo /sbin/ifup --force wlan0'], shell=True)
            WLAN_check_flg = False

#call function

4) does this script look effective? Is there anything obvious that may cause problems, that anyone, other than me, can see?


1 Answer 1


1) This instruction pings and it's result is piped to grep. Grep, subsequently, looks for the output "1 received". If that sentence is in there, it means that the host is up and responded to the ping command.

You can replicate this command. The issue is that the output is being redirected to /dev/null, hence you can't see anything!

Try running:

ping -c 2 -w 1 -q |grep "1 received"

If the host is up, you should see something like "1 packets transmitted, 1 received, 0% packet loss, time 0ms". If so, this command will return true and enter the next if in your script.

2) logger is a shell command interface for the syslog module. Run "man logger" in a terminal and check it out. It should be storing the logs somewhere in /var/log (typically the 'messages' file).


  • Shutdown the wlan0 interface (aka wireless)
  • Wait 10 seconds
  • Bring the wlan0 interface back up

4) Test it. It looks good to me, although I am not sure if there isn't a way for the operating system to do it automatically

Edit: More info on logger

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.