Django Manage.py 外部脚本错误
我写了一个 manage.py 的命令,用来通过 ftplib 下载文件。如果我在终端直接运行这个命令,一切都很正常。现在我想从一个脚本中调用这个命令,这样 cron 就可以每晚自动运行它。但是,当我运行这个脚本时,它出现了“django.db.utils.DatabaseError: no such table”的错误。我可以在外部脚本中无错误地运行 manage.py 和 manage.py help 命令。有什么想法吗?
来自管理命令的 handle 方法:
def handle(self, *args, **options):
ftp = ftplib.FTP('ftp.somesite.com')
ftp.login("anonymous")
ftp.cwd('commonupdater')
data = []
ftp.dir(data.append)
file_lines = [line for line in data if 'mydat' in line and '.zip' in line]
files = [line for line in file_lines]
for filespec in files:
target = filespec.split(' ')[-1]
f = open('/tmp/' + target, 'wb')
self.getbinary(ftp, target, outfile=f)
f.close()
self.save_update(target, '/tmp/' + target)
错误追踪信息
local.dbTraceback (most recent call last):
File "/home/rb/Workspaces/rms/ravelin/manage.py", line 14, in <module>
execute_manager(settings)
File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/home/rb/Workspaces/rms/ravelin/ravelog/management/commands/fetch_av_updates.py", line 57, in handle
self.save_update(target, '/tmp/' + target)
File "/home/rb/Workspaces/rms/ravelin/ravelog/management/commands/fetch_av_updates.py", line 30, in save_update
if len(av_list) < 1:
File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/db/models/query.py", line 82, in __len__
self._result_cache = list(self.iterator())
File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/db/models/query.py", line 273, in iterator
for row in compiler.results_iter():
File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 680, in results_iter
for rows in self.execute_sql(MULTI):
File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
cursor.execute(sql, params)
File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/db/backends/util.py", line 34, in execute
return self.cursor.execute(sql, params)
File "/home/rb/.virtualenvs/ravelin/lib/python2.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:
1 个回答
0
你能试试这个吗?找出在命令行中使用的环境变量(用set命令可以列出这些变量)。把这些变量放到一个shell脚本里,然后在最后调用你的python脚本。在cron中,像下面这样调用这个shell脚本。日志文件应该包含所有的细节。
10 5 * * * /home/myuser/some_wrapper_script.sh > /home/myuser/some_wrapper_script.log 2>&1