调度django自定义命令 [英] Scheduling django custom command
问题描述
我很难让我的自定义命令按计划运行.我尝试了 cronjob 和 django-chronograph,但我似乎无法做到像它可以(成功)从命令行一样运行.
I am having difficulty getting my custom command to run on schedule. I have tried a cronjob and django-chronograph, but I can't seem to get it to run like it can (successfully) from the command line.
我只是使用安装在 Ubunutu 上的 django 在本地开发一个应用程序.
I'm just developing an app locally using django installed on Ubunutu.
我有一个自定义命令,用于清除超过 30 天的日志条目.为了反复测试,我修改了它,让它只删除一个有点随意的条目:
I have a custom command that I use to clear out log entries that are greater than 30 days old. In order to test it repeatedly, I altered it so that it only deletes one somewhat arbitrary entry:
from datetime import datetime, timedelta
from hitcount.models import Hit
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
args = '<days>'
help = 'Clear out all Hits that occured over "days" days ago'
def handle(self, *args, **options):
list = Hit.objects.filter(created__lt = datetime.now()-timedelta(days=2) )
list[0].delete()
self.stdout.write('Hit deleted - %s' % list[0])
这个命令(del_hit.py)位于以下结构中:
This command (del_hit.py) is located in the following structure:
/project_dir
/app
/management
__init__.py
/commands
__init__.py
del_hit.py
正如我所说,我可以从我的 project_dir 成功运行此命令
As I stated, I can successfully run this command from my project_dir
python manage.py del_hit
我尝试安装 django-chronograph.它似乎非常有用.它识别出我的命令并允许我从下拉列表中选择它.我按照指示应用了 cron:
I tried installing django-chronograph. It seems very useful. It recognized my command and allowed me to select it from a drop down list. I applied the cron as instructed:
- /home/vadmin/development/python/my_proj/manage.py cron
但是,当它尝试执行命令时,它在日志中给了我以下错误:
However, when it tries to execute the command, it gives me the following error in the log:
The job failed to run. The exception was : Unknown command: u'del_hit' Traceback (most recent call last): File "/home/vadmin/development/python/my_proj/chronograph/models.py", line 213, in handle_run call_command(self.command, *args, **options) File "/usr/lib/python2.6/dist-packages/django/core/management/__init__.py", line 155, in call_command raise CommandError("Unknown command: %r" % name) CommandError: Unknown command: u'del_hit'
我尝试从标准视图自己运行命令:
I tried running the command myself from a standard view:
from django.core.management import call_command def test_del(request): list = Hit.objects.filter(created__lt = datetime.now()-timedelta(days=2) ) args = [] options = {} call_command('del_hit', *args, **options) return render_to_response('test.html', {'del_hit_item':list[0]})
这个确实执行成功了.
最后,我尝试设置一个 cronjob 每小时执行一次
Finally, I tried to setup a cronjob to execute every hour
30 * * * * python /home/vadmin/development/python/my_proj/manage.py del_hit
这不起作用.
我可以使用 django-chronograph(最好)或简单的 cronjob 来帮助我让我的自定义命令按计划运行
推荐答案
看起来你需要在你的项目目录中才能正常工作.
It looks like you need to be in the your project directory for this to work correctly.
首先尝试更新您调用的命令以执行 'cd',
Try updating the command you call to do a 'cd' first,
(cd/home/vadmin/development/python/my_proj && ./manage.py del_hit)
这篇关于调度django自定义命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!