crontab不会查询Python MySQLdb

时间:2014-11-20 17:36:56

标签: python mysql crontab lamp

我使用名为MySQLdb的库连接到Python中的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


Syslog看起来像这样:

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 &


hosts文件看起来像这样

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只是为了消除该问题。 我搜索过类似的问题,并为他们解决了问题,但这里没有运气。 那我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

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

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



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