Python和crontab中的客户端SQL

时间:2022-09-22 12:26:18

I have some script in Python which are executed by crontab on Raspbian. I use the MySQLdb library for request in the local network.

我在Python中有一些脚本,由Raspbian上的crontab执行。我在本地网络中使用MySQLdb库请求。

All scripts work fine if i launch them directly, by Python-IDLE, or in the console. But if they are launched by cron, those who just execute "INSERT" requests work, but those who execute "SELECT" request don't work.

如果我直接通过Python-IDLE或在控制台中启动它们,所有脚本都可以正常工作。但是如果它们是由cron启动的,那些只执行“INSERT”请求的人会工作,但那些执行“SELECT”请求的人不起作用。

I haven't found a clear solution, but it seems that crontab doesn't execute the same configuration of the SQL client as the user. Maybe i have to change the path before all request ? (looking for a "./my.cnf" ?

我还没有找到一个明确的解决方案,但似乎crontab没有执行与用户相同的SQL客户端配置。也许我必须在所有要求之前改变路径? (寻找“./my.cnf”?

tested with library MySQLdb and PyMySQL

用MySQLdb和PyMySQL库测试

#! /usr/bin/python
# -*- coding: utf-8 -*-

# importations  
import os
import time
import sys
import pymysql as sql  # or MySQLdb as...
from os import path as os_path

#-----------------------------------------------------------------#
#  constants : use your own values / utilisez vos propres valeurs #
#-----------------------------------------------------------------#
PATH_THERM = "/home/pi/Documents/" #path to this script
DB_SERVER ='192.168.0.59'       # MySQL : IP server 
DB_USER='user'                    # MySQL : user
DB_PWD='password'              # MySQL : password
DB_BASE='capteurs'              # MySQL : database name

def log(texte):
    datation = time.strftime('%d-%m-%Y %H:%M:%S')

    logue = open('log_test.txt','a')
    txt = "\n" + datation + "\t" + texte
    txt = txt.encode('utf-8')
    logue.write(txt)
    logue.close()

def query_temp():
    datebuff = time.strftime('%d-%m-%Y')

    db = sql.connect(DB_SERVER, DB_USER, DB_PWD, DB_BASE)
    cursor = db.cursor()
    cursor.execute("""SELECT sonde2,date FROM `PiTemp` ORDER BY date DESC LIMIT 0, 1""")
    rows = cursor.fetchall()
    print datebuff, u" : Dernière température de l'eau :", rows[0][0], u"°C"
    log(u"lecture température SQL - ok")

    a = rows[0][0]
    b =  rows[0][1]
    return (a, b)


#----------------------------------------------------------#
#             principal code                               #
#----------------------------------------------------------#

PATH=os_path.abspath(os_path.split(__file__)[0])
os.chdir(PATH)
log('start')
log(PATH)
txt = str(query_temp()[0])
log(txt)

crontab :

*/1 * * * * python /home/pi/Documents/180623_test.py

1 个解决方案

#1


0  

Found raeson of fail, the print passed by cron don't encode/decode the same as the console (!??).

发现失败的raeson,cron传递的print不会像控制台那样编码/解码(!??)。

... print (u"Alerte, tous à poil !") UnicodeEncodeDecode Error !

...打印(你是“Alerte,tousàpoil!”)UnicodeEncodeDecode错误!

Strange, looks like cron doesn't encode / decode the same than the user.... (!?)

奇怪的是,看起来像cron没有比用户编码/解码相同....(!?)

Solved with no latin characters in print, or print passed by try/except...

解决了打印时没有拉丁字符,或通过try / except传递的打印...

#1


0  

Found raeson of fail, the print passed by cron don't encode/decode the same as the console (!??).

发现失败的raeson,cron传递的print不会像控制台那样编码/解码(!??)。

... print (u"Alerte, tous à poil !") UnicodeEncodeDecode Error !

...打印(你是“Alerte,tousàpoil!”)UnicodeEncodeDecode错误!

Strange, looks like cron doesn't encode / decode the same than the user.... (!?)

奇怪的是,看起来像cron没有比用户编码/解码相同....(!?)

Solved with no latin characters in print, or print passed by try/except...

解决了打印时没有拉丁字符,或通过try / except传递的打印...