【问题标题】:crontab with sudo python script带有 sudo python 脚本的 crontab
【发布时间】: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 中正常调用脚本

标签: python crontab sudo


【解决方案1】:

找出问题所在。

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

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

【讨论】:

  • 好吧,当我在命令行中输入“sudo python script.py”时,脚本运行良好(我是 ssh)。它将输出打印到屏幕上,我可以看到谷歌电子表格创建一个新行,bam!数据在那里。我似乎无法让它在 crontab 中运行,现在我从 /etc/init.d 调用了一个单独的 bash 脚本,但这也不起作用。有趣的是,使用/etc/init.d/script start,它启动了python脚本,但不在后台。我正在使用为我的 python 脚本量身定制的 noip2 init.d 脚本示例。
  • 我已经从我的 /home/pi 目录和 /usr/local/bin 目录运行它(其中也有 Adafruit_DHT c 程序)。它似乎只有在我手动输入命令时才会运行。它只是拒绝在后台通过 crontab 或 /etc/init.d... :(
  • 不知道该说什么。看来您要么没有阅读我的答案,要么只是忽略了它。
  • 好的,所以在回答你答案 1 的第一部分时,我认为这已经足够了。但是再看一遍,也许你明白了,但意思是“它在 crontab 中激活时会运行吗?”嗯...好吧,我查看了 /var/log/syslog 并且可以看到创建的实例具有一些与我在 crontab 中运行的另一个脚本(成功,但没有 sudo)几乎相同的信息.只是什么都没有产生。我想我必须重新分析你的第 1 步和第 2 步,并尝试弄清楚你的意思以及如何去做。
【解决方案2】:

试试:

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

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

【讨论】:

  • 只要脚本有正确的#! 行,就没有必要通过/usr/bin/python 显式调用它。
  • 从我过去的经验来看,它并不总是适用于#!。安全总比后悔好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-11
  • 2017-08-06
  • 2015-10-05
  • 2017-12-16
  • 1970-01-01
相关资源
最近更新 更多