Lucas Ou-Yang
Lucas Ou-Yang

Reputation: 5655

Running django standalone scripts in the crontab destroys any import commands related to django

I have tried many many solutions to fixing this, including using django's custom management commands, python scripts with path configuration and setting the DJANGO_ENV_VAR, etc. Everything breaks when I try to import something from django specifically, like:

from django.db

or

import django.utils

Here is an example of one of my error tracebacks:

Traceback (most recent call last): 
File "/home/username/webapps/folder/Project/scripts/standalone.py", line 29, in 
<module> from Model.models import Model File "/home/username/webapps/folder/
Project/App/models.py", line 15, in <module> from django.db import models
ImportError: No module named django.db

In the crontab I followed all the general procedures. All of the time configs are correct, all the scripts are being called with their full paths, ie. /usr/bin/python2.7 or /usr/bin/sh.

When any of these scripts are run in the shell and not in crontab, it works just fine: like:

python2.7 manage.py killModels

or

sh scrapper.sh >> log.log

Here is the crontab:

8,38,58 * * * * ~/webapps/Folder/apache2/bin/start
#*/1 * * * * /bin/sh ~/webapps/Folder/Project/scripts/unpack.sh >> ~/webapps/Folder/Project/logs/unpack.log 2>&1
*/25 * * * * ~/bin/indexer Model_Model --rotate --config    
~/webapps/Folder/Project/misc/sphinx/sphinx.conf >> ~/webapps/Folder/Project/logs/searchd_log.txt
*/25 * * * * /bin/sh ~/webapps/Folder/Project/scripts/check_sphinx.sh >> ~/webapps/Folder/Project/logs/searchd_log.txt
0 2 * * * mysqldump --defaults-file=~/db_backups/Project_db.cnf -u Project_db Project_db >  ~/db_backups/Project_db-`date +\%Y\%m\%d`.sql 2>> ~/db_backups/cron.log
*/1 * * * * /usr/local/bin/python2.7 ~/webapps/Folder/Project/manage.py UpdateModels >> ~/webapps/Folder/Project/logs/unpack.log 2>&1

Anyone have any ideas? Thanks.

Upvotes: 0

Views: 573

Answers (1)

dani herrera
dani herrera

Reputation: 51675

Replace:

*/1 * * * * /usr/local/bin/python2.7 \
            ~/webapps/Folder/Project/manage.py UpdateModels \
            >> ~/webapps/Folder/Project/logs/unpack.log 2>&1

by:

*/1 * * * * (cd ~/webapps/Folder/Project/; \
             /usr/local/bin/python2.7 manage.py UpdateModels) \
             >> ~/webapps/Folder/Project/logs/unpack.log 2>&1

With this change manage.py would be able to locate django applications.

Upvotes: 1

Related Questions