pythonpython-3.xcronraspbianiwconfig

iwconfig unavailable to processes started with crontab @reboot?


this is my first post to stackoverflow, so please bear with me :)

I'm trying to read the output of iwconfig from a python script to determine whether there is a wifi connection. When I run script (via a bash script that first sets the directory) using crontab @reboot (user, not root), subprocess.check_output(['iwconfig']) always throws an [Errno 2]. This is even true when I catch the error using try/except and loop the code, so it is still running when the Wifi is certainly connected (as I can check with running iwconfig manually). When I run the python script from the command line via the same bash script, it works fine. What am I overlooking?

#!/usr/bin/python3

import subprocess
import time
import logging

logging.basicConfig(filename='wifi_check.log', filemode='w', format='%(name)s - %(levelname)s 
    - %(message)s', level=logging.DEBUG)

logging.info("Checking for Wifi")

for i in range(20):

    try:
        iwconfig_output = subprocess.check_output(['iwconfig']).decode('utf-8')
    except Exception as err:
        logging.error(str(i) + str(err))
    else:
        logging.debug(str(i) + iwconfig_output)
        if "ESSID" in iwconfig_output:
            logging.info(str(i) + "Wifi active")

    time.sleep(10)

Solution

  • Errno 2 can indicate that the file is not found. Perhaps iwconfig is not in PATH for the user who executes the script. Try using /sbin/iwconfig (the full path) of the executable to rule this out.