0

我尝试了许多解决方案来解决这个问题,包括使用 django 的自定义管理命令、带有路径配置的 python 脚本和设置 DJANGO_ENV_VAR 等。当我尝试从 django 导入某些东西时,一切都会中断,比如:

from django.db

或者

import django.utils

这是我的错误回溯之一的示例:

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

在 crontab 中,我遵循了所有一般程序。所有时间配置都是正确的,所有脚本都以其完整路径调用,即。/usr/bin/python2.7 或 /usr/bin/sh。

当这些脚本中的任何一个在 shell 而不是在 crontab 中运行时,它工作得很好:比如:

python2.7 manage.py killModels

或者

sh scrapper.sh >> log.log

这是 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

有人有想法么?谢谢。

4

1 回答 1

1

代替:

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

经过:

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

有了这个改变 manage.py就可以定位 django 应用程序。

于 2013-07-22T19:39:16.413 回答