Domanda

I have OpenELEC installed on two Raspberry Pis. I have set up a Python script on one of the Pis that opens a network socket and listens for a connection. The following code is from a script that runs on the other Raspberry Pi and connects to the listener to send a message:

# Run the command on the local system to play the show
os.system(command)

# Set up network information for sending data and connect
r_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Open the client list file
clientlist = open("clients.txt", "r")

for line in clientlist.readlines():
    try:
        client = line.rstrip('\n').split(':')
        r_socket.connect((client[0], int(client[1])))
    except:
        print("Failed to connect to remote device")
    else:
        # Hash IP and MAC addresses
                hash = hashlib.sha224(r_socket.getsockname()[0] + " " + getHwAddr('eth0')).hexdigest()

                # Send the message to other RPis on the network
                r_socket.send(hash + "\n" + command)

# Close the socket when finished
r_socket.close()

Finally I have a crontab entry that runs the script at certain times of the day. It executes the first half of the script properly but fails once it reaches the network section. If I run the script manually it functions properly and sends the message to the listener Pi.

From what I understand there is only one account on the Pi (root) and the script can be run by all users (chmod a+x myscript.py). So, I don't think it is a permissions issue but I can't figure out what the problem is.

Does anyone know what might cause the network portion of the script to fail when executed by cron but not when it is manually run?

È stato utile?

Soluzione

You should use absolute paths for clients.txt and command. The cron execution environment will most likely not be the same as your shell environment (different environment variables, different working directory).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top