Вопрос

I have a Python script that connects with MySQLdb that runs perfectly fine when I enter it on command line but fails when trying to run it via cronjob. The cronjob statement can be assumed right as its calling the script but the script is utterly failing trying to connect with the database.

Do I need to open some sort of permissions for mysql to have a cronjob interact? The file permissions are completely open chmod 777 for the script. I connect to the DB using the statement (which says it has connected):

import MySQLdb as mdb
mdb.connect(read_default_file="./my.cnf")

But then when I try to select a known database .select_db("known_db") it says it cannot be found and if I try and create it using a .query() statement the script completely fails. Ideas?

Это было полезно?

Решение

that's should be because your cron execution directory is not the same then your working directory

try to change your line in cron from

*  *  *   *  *  python /path/to/your/script/script.py

to

*   *   *   *   * cd /path/to/your/script/ && python script.py

or you can change your script:

import os
import MySQLdb as mdb
mdb.connect(read_default_file=os.path.realpath(__file__)+"/my.cnf")
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top