我创建了自定义django-admin命令。我使用本教程Writing custom django-admin commands
例如,我的命令:
import os
from home.models import BuildTask
class Command(BaseCommand):
def handle(self, *args, **options):
tasks = BuildTask.objects.all()
os.system("touch /Users/macbook/Desktop/Start.txt")
if tasks:
os.system("touch /Users/macbook/Desktop/TasksExist.txt")
else:
os.system("touch /Users/macbook/Desktop/TasksNotExist.txt")
os.system("touch /Users/macbook/Desktop/End.txt")
当我通过使用终端运行此命令时:
python manage.py build_task
一切正常,我在Destop上获得3个文件。
但是当我使用crontab时,
* * * * * /usr/bin/python /Users/macbook/builder/manage.py build_task
我只在桌面上获得1个文件(“Start.txt”)。显然,脚本会停止工作。
if tasks:
请帮帮我。有什么问题?
更新: / var / mail / macbook:
File "/Users/macbook/workspace/acodebuilder/home/management/commands/build_apk.py",
line 26, in handle
if tasks:
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 113, in
__nonzero__
iter(self).next()
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 107, in
_result_iter
self._fill_cache()
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 772, in
_fill_cache
self._result_cache.append(self._iter.next())
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 273, in
iterator
for row in compiler.results_iter():
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 680, in
results_iter
for rows in self.execute_sql(MULTI):
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 735, in
execute_sql
cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/util.py", line 34, in
execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py", line 234,
in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.DatabaseError: no such table: home_buildtask
为什么“没有这样的表”?
答案 0 :(得分:1)
您应该能够通过运行mail
命令查看命令的错误输出(如果有的话)。或者,您可以将&> ~/Desktop/django-command-error.log
附加到crontab条目,并且cron作业的错误输出将保存到桌面上的django-command-error.log
。
答案 1 :(得分:0)
这是由SQLite db引起的问题,我们必须使用MySQL或PGSQL!