나는 MySQLdb라는 라이브러리를 사용하여 Python에서 mysql에 연결합니다.이 라이브러리는 터미널에서 잘 실행됩니다. 재부팅 할 때 crontab이 동일한 파일을 실행하게하면 데이터베이스에 값이 입력되지 않습니다.crontab은 Python MySQLdb를 쿼리하지 않습니다
PHP 연결에 문제가 없습니다.
파이썬 파일은 다음과 같다 :
#! /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 [email protected] on temp
47 Query set autocommit=0
47 Quit
141120 18:05:01 48 Connect [email protected] on temp
48 Query set autocommit=0
48 Quit
141120 18:06:01 49 Connect [email protected] on temp
49 Query set autocommit=0
49 Quit
141120 18:07:01 50 Connect [email protected] 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 파일은 그냥 그 문제를 제거하기 위해 7777에 pythonfiles 권한을 설정할이
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
처럼 보이는이
* * * * * cd /home/ && python tempsensor.py &
hosts 파일처럼 보인다. 비슷한 질문을 검색하여 문제를 해결했지만 행운은 없습니다. 그래서 내가 여기서 무엇을 그리워합니까?
에
* * * * * cd /home/ && python tempsensor.py &
을 변경했다? – Andy
@Andy 질문을 업데이트했습니다. – EL3PHANTEN
tryInsert를 호출하기 직전에 커서를 연결하고 연결을 시도하여 MySQL에 연결이되었는지 확인하십시오. – spicavigo