user2417713
user2417713

Reputation: 167

Raspberry Pi Crontab script error

I have the following script on a Raspberry Pi to send an SMS. It runs if I type: python tides_sms.py

The problem is, I can't get it to run via Crontab (* * * * * /usr/bin/python /home/pi/python_files/tides_sms.py). The file is set to: rwxr-xr-x

When I add code to write to a file, the file gets created via Crontab, but it won't send an SMS.

Any help appreciated.


#!/usr/bin/python

from twilio.rest import TwilioRestClient

# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "**********************************"
auth_token = "********************************"

with open("tide_data.txt", "r") as file:
    tides_array = file.read().splitlines()

tides_array.reverse()

elements = tides_array[0].split(' | ')

string=''
for element in elements:
    string = '\n'.join([string, element])

client = TwilioRestClient(account_sid, auth_token)

message = client.sms.messages.create(body="Text from PI:\nTIDES" + string,
    to="+44??????????",
    from_="+44??????????")

Upvotes: 2

Views: 806

Answers (1)

hek2mgl
hek2mgl

Reputation: 158110

When a script is running via cron the working directory is / - the filesystem root. Use absolute paths in the script:

with open("/path/to/tide_data.txt", "r") as file:

Upvotes: 3

Related Questions