All scripts work fine if i launch them directly from 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.
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 of the console. Maybe i have to change the path before all request ? (looking for a "./my.cnf" ) ?
Maybe i have to "force" the user, but i thought that
"crontab -e" is for the user, and "sudo crontab -e" with root rights.
unsuccessfull tries :
tested with libraries MySQLdb and PyMySQL
tested with "sudo python /home/pi/Documents/..."
tested on Jessie '(Raspi 2), on Stretch (Raspi 3B+)
tested with the script as a service
tested with "sudo crontab -e"
an example code below (on a local network) :
Code: Select all
#! /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 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, u"°C" log(u"lecture température SQL - ok") a = rows b = rows return (a, b) # principal code #------------------------------------- PATH=os_path.abspath(os_path.split(__file__)) os.chdir(PATH) log('start') log(PATH) txt = str(query_temp()) log(txt)
and the corresponding crontab :
Code: Select all
*/1 * * * * python /home/pi/Documents/180623_test.py