I'm working on a Python Twitter bot that works fine in the Python editor and my Raspberry Pi's terminal, but when I run it using cron, I get an error.
crontab file:
* * * * * /home/eric/code/quotebot/quotebot.py >> /home/eric/code/quotebot/logs/minute.log 2>&1
Error message:
Traceback (most recent call last):
File "/home/eric/code/quotebot/quotebot.py", line 22, in <module>
cursor.execute("SELECT MIN(countoftweets) FROM quotebot")
sqlite3.OperationalError: no such table: quotebot
Because it works when running from the terminal and IDE, I'm wondering if there's an issue with permissions or something else I'm not aware of. The table definitely exists and the database definitely exists.
CodePudding user response:
If this is your local user crontab, then when the script runs, the current directory is set to your home directory, /home/eric
. You probably did something like sqlite3.connect('quotebot.db')
. That file didn't exist, and sqlite3 is happy to create a new empty DB for you.
If you need files outside of your home, you'll either have to use absolute paths, or derive the path of the script by using os.path.dirname(__file__)
.
CodePudding user response:
I kept hacking away at it and found someone had a similar problem, and it was answered here.
I had to change the path of my database file from: path.db to /directory/directory/path.db
Now it works!