'Python Crontab Can't Read Sqlite3 Table
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.
Solution 1:[1]
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__).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Roberts |
