计划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:
-
-
- ul>
-
- /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'
我试图从标准视图自己运行命令: p>
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 按计划运行
推荐答案
尝试更新您调用的命令以首先执行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屋!
- ul>
-