python - crontab 不会查询 Python MySQLdb

标签 python mysql cron lamp

我使用 Python 中的一个名为 MySQLdb 的库连接到 mysql,该库从终端运行良好。 当我重新启动并让 crontab 运行同一文件时,没有任何值输入到数据库中。

PHP 连接没有问题。

python 文件如下所示:

#! /usr/bin/env python
import time
import datetime
import os

import RPi.GPIO as GPIO
import sys; sys.path.append('/usr/lib/python2.7/dist-packages/MySQLdb/')
import MySQLdb as mdb

GPIO.setmode(GPIO.BCM)

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

sensor = ['/sys/bus/w1/devices/28-0000059ff0ae',
  '/sys/bus/w1/devices/28-000005a0218e',
  '/sys/bus/w1/devices/28-000005a0486f',
  '/sys/bus/w1/devices/28-000005a10dc2',
  '/sys/bus/w1/devices/28-000005a13c68']

def read_temp_raw(fil):
  f = open(sensor[fil]+'/w1_slave', 'r')
  lines = f.readlines()
  f.close()
  return lines

def read_temp(fil):
  lines = read_temp_raw(fil)
  while lines[0].strip()[-3:] != 'YES':
      time.sleep(0.2)
      lines = read_temp_raw()
  equals_pos = lines[1].find('t=')
  if equals_pos != -1:
      temp_string = lines[1][equals_pos+2:]
      temp_c = float(temp_string) / 1000.0
      return temp_c

def tryInsert(col,val):
  cur.execute("INSERT INTO temps("+col+") VALUES"+str(val))
  con.commit()

tid = str(int(time.mktime(datetime.datetime.now().timetuple())))

con = mdb.connect('localhost', 'root', 'something', 'temp')
cur = con.cursor()
tryInsert("time,temp1,temp2,temp3,temp4,temp5",str((tid,read_temp(0),read_temp(1),read_temp(2),read_t    emp(3),read_temp(4))))

con.close()


在mysql日志中我可以看到它每分钟连接一次,但不查询。 因此 crontab 执行 python 文件。

141120 18:04:02    47 Connect   root@localhost on temp
       47 Query set autocommit=0
       47 Quit  
141120 18:05:01    48 Connect   root@localhost on temp
       48 Query set autocommit=0
       48 Quit  
141120 18:06:01    49 Connect   root@localhost on temp
       49 Query set autocommit=0
       49 Quit  
141120 18:07:01    50 Connect   root@localhost on temp
       50 Query set autocommit=0
       50 Quit


系统日志如下所示:

Nov 20 19:17:01 raspberrypi /USR/SBIN/CRON[2894]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Nov 20 19:18:01 raspberrypi /USR/SBIN/CRON[2910]: (root) CMD (cd /home/ && python tempsensor.py &)
Nov 20 19:19:01 raspberrypi /USR/SBIN/CRON[2924]: (root) CMD (cd /home/ && python tempsensor.py &)
Nov 20 19:20:01 raspberrypi /USR/SBIN/CRON[2931]: (root) CMD (cd /home/ && python tempsensor.py &)
Nov 20 19:21:01 raspberrypi /USR/SBIN/CRON[2949]: (root) CMD (cd /home/ && python tempsensor.py &)
Nov 20 19:22:01 raspberrypi /USR/SBIN/CRON[2957]: (root) CMD (cd /home/ && python tempsensor.py &)


如果我从终端运行 pythonfile,mysql 日志如下所示。

141120 18:57:06    72 Query INSERT INTO temps(time,temp1,temp2,temp3,temp4,temp5)   VALUES('1416506222', 23.0, 22.75, 23.0, 22.812, 23.0)
       72 Query commit
       72 Quit  


crontab 文件看起来像这样

* * * * * cd /home/ && python tempsensor.py &


主机文件看起来像这样

127.0.0.1   localhost
::1         localhost ip6-localhost ip6-loopback
fe00::0     ip6-localnet
ff00::0     ip6-mcastprefix
ff02::1     ip6-allnodes
ff02::2     ip6-allrouters

127.0.1.1   raspberrypi


我将 pythonfiles 权限设置为 7777 只是为了消除这个问题。 我搜索过类似的问题,并做了什么解决了他们的问题,但这里没有运气。 那么我在这里想念什么呢?

最佳答案

解决这个问题的方法是改变

* * * * * cd/home/&& python tempsensor.py &



* * * * * cd/home/&& sudo python tempsensor.py &

关于python - crontab 不会查询 Python MySQLdb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27045978/

相关文章:

python - gaierror : [Errno 8] nodename nor servname provided, 或未知(使用 macOS Sierra)

python - MongoDB - 使用 PyMongo 清除缓存

MySQL 按 "best match"排序

php - MySQL任务的即时可视化

linux - Linux 上的定时任务

python - 如何使用 Python 和 matplotlib 获取动画补丁而不是 n 次绘制的补丁

python - 列出将从 requirements.txt 安装的 Python 包

MySQL的不同引号

Python 解决方案允许通过电子邮件将照片上传到我的 Django 网站

php - Supervisor VS CronJobs