2013-09-28 193 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