繁体   English   中英

使用sudo python脚本的crontab

[英]crontab with sudo python script

好吧,我发现了一些东西。 不确定如何解决。 我已经看到这是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)添加,但无济于事...

找到问题所在。

  1. 脚本是否完全运行? 在脚本的第一行中做一些琐碎的事情,以查看它实际上是从cron执行的(例如:写入/ tmp中的文件)。

  2. 一旦您设法运行它,寻找其他问题。 可以设置Cron以发送带有脚本输出的邮件。 我看到的一个显而易见的事情是: ./Adafruit_DHT ,使用了相对路径,这是一个不好的信号,cron脚本可能未在您认为要执行的目录中执行。 修复它(=使用绝对路径)。

尝试:

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

哦,将您的cron行更改为:/ usr / bin / python /home/pi/myscript.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM