2013-07-27 157 views
0

好的,我找到了一些東西。不知道如何解決它。我已經看到這是谷歌出現的一個常見錯誤。這個錯誤似乎與環境變量或某事有關。不知道如何處理這個問題:使用sudo python腳本的crontab

這是代碼,它也正是子被稱爲導致錯誤的部分:

#!/usr/bin/python 

import subprocess 
import re 
import sys 
import time 
import datetime 
import gspread 

# =========================================================================== 
# Google Account Details 
# =========================================================================== 

# Account details for google docs 
email  = '[email protected]' 
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)的完整路徑,都無濟於事......

+0

您應該看到'/ VAR錯誤/日誌/ syslog'。有沒有? – twil

+0

你的腳本根本不運行,或者它運行但不寫入谷歌電子表格? – Verma

+0

cron運行的用戶是什麼?它是在sudoers?你可以將python腳本包裝在一個調用sudo的bash腳本中嗎? –

回答

0

找到問題。

  1. 腳本是否可以運行? 在腳本的第一行做一些小事,看它實際上是從cron(例如:寫入/ tmp中的文件)執行的。

  2. 一旦你設法運行它,尋找其他問題。 Cron可以設置爲發送帶有腳本輸出的郵件。我看到的一件顯而易見的事情是:./Adafruit_DHT,使用相對路徑,這是一個不好的跡象,cron腳本可能不會在您認爲它執行的目錄中執行。修復它(=使用絕對路徑)。

+0

好吧,當我在命令行鍵入'sudo python script.py'(我是ssh'd)時,腳本運行的非常漂亮。它打印輸出到屏幕上,我可以看到谷歌電子表格創建一個新的行,巴姆!數據在那裏。我似乎無法得到它在crontab中運行,現在我有一個單獨的bash腳本從/etc/init.d調用,並且也不起作用。有趣的是,使用/etc/init.d/script start,它啓動python腳本,但不在後臺。我正在使用爲我的python腳本定製的noip2 init.d腳本示例。 – rickman90

+0

我已經從我的/ home/pi目錄和/ usr/local/bin目錄中運行它(也有Adafruit_DHT c程序)。它似乎只能運行,如果我手動輸入命令。它只是拒絕通過crontab或/etc/init.d在後臺執行它...... :( – rickman90

+0

不知道該說些什麼。看起來好像你要麼沒有閱讀我的答案,要麼完全忽略它。 –

0

嘗試:

output = subprocess.check_output(["PATH_TO_Adafruit_DHT/Adafruit_DHT", "2302", "17"]); 

哦,你的cron線更改爲: 在/ usr/bin中/ Python的/home/pi/myscript.py

+0

只要腳本有一個正確的'#!'行,它就不是必須明確地通過'/ usr/bin/python'來調用它。 –

+0

從我的經驗過去它並不總是和'#!'一起工作。比對不起更安全。 –