2013-09-28 77 views
2

我在Raspberry Pi上有以下腳本來發送短信。它運行,如果我輸入:python tides_sms.pyRaspberry Pi Crontab腳本錯誤

問題是,我不能讓它通過Crontab運行(* * * * */usr/bin/python /home/pi/python_files/tides_sms.py )。該文件設置爲:rwxr-xr-x

當我添加代碼寫入文件時,該文件通過Crontab創建,但不會發送SMS。

任何幫助表示讚賞。


#!/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??????????") 

回答

3

當腳本通過cron運行的工作目錄是/ - 文件系統根目錄。在腳本中使用絕對路徑:

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

謝謝你。簡單! – user2417713

+0

沒問題!是的,這是一個常見的陷阱:) – hek2mgl