【发布时间】:2013-07-27 15:09:27
【问题描述】:
好的,我找到了一些东西。不知道如何解决它。我已经看到这是 google 中出现的常见错误。该错误似乎与环境变量或其他东西有关。不知道如何处理:
这是代码,它是调用子进程的部分导致错误:
#!/usr/bin/python
import subprocess
import re
import sys
import time
import datetime
import gspread
# ===========================================================================
# Google Account Details
# ===========================================================================
# Account details for google docs
email = 'my_email@gmail.com'
password = 'my_password'
spreadsheet = 'my_spreadsheet'
# ===========================================================================
# Example Code
# ===========================================================================
# Login with your Google account
try:
gc = gspread.login(email, password)
except:
print "Unable to log in. Check your email address/password"
sys.exit()
# Open a worksheet from your spreadsheet using the filename
try:
worksheet = gc.open(spreadsheet).sheet1
# Alternatively, open a spreadsheet using the spreadsheet's key
# worksheet = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE')
except:
print "Unable to open the spreadsheet. Check your filename: %s" % spreadsheet
sys.exit()
# Continuously append data
while(True):
# Run the DHT program to get the humidity and temperature readings!
output = subprocess.check_output(["./Adafruit_DHT", "2302", "17"]);
print output
matches = re.search("Temp =\s+([0-9.]+)", output)
if (not matches):
time.sleep(3)
continue
temp1 = float(matches.group(1))
temp = temp1*9/5+32 # added the extra step to converto to fahrenheit
# search for humidity printout
matches = re.search("Hum =\s+([0-9.]+)", output)
if (not matches):
time.sleep(3)
continue
humidity = float(matches.group(1))
print "Temperature: %.1f F" % temp
print "Humidity: %.1f %%" % humidity
# Append the data in the spreadsheet, including a timestamp
try:
values = [datetime.datetime.now(), temp, humidity]
worksheet.append_row(values)
except:
print "Unable to append data. Check your connection?"
sys.exit()
# Wait 30 seconds before continuing or just exit
print "Wrote a row to %s" % spreadsheet
# time.sleep(60)
sys.exit()
基本上就是这样。只要 Adafruit_DHT 程序位于同一目录中,使用“sudo python script.py”就可以正常工作。
这是我得到的错误:
Traceback (most recent call last):
File "/home/pi/Adafruit/dht/Ada_temp.py", line 44, in <module>
output = subprocess.check_output(["./home/pi/Adafruit/dht/Adafruit_DHT", "2302", "17"]);
File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我已经尝试添加c程序的完整路径(Adafruit_DHT),但无济于事......
【问题讨论】:
-
您应该会在
/var/log/syslog中看到错误。有吗? -
您的脚本是否根本没有运行,或者它运行但没有写入谷歌电子表格?
-
cron 以什么用户身份运行?是在 sudoers 中吗?你能把 python 脚本包装在一个调用 sudo 的 bash 脚本中吗?
-
在 syslog 中不多,与另一个有效的 cron 相同的消息。当使用 sudo 输入(cron 以 root 用户身份运行)时,该脚本工作正常。我将不得不研究做 bash 脚本...
-
crontab有自己的环境。尝试指定 python 的绝对路径。另外,不要在 crontab 中调用sudo。如果您必须以 root 身份运行它,请从sudo crontab中正常调用脚本