Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

cron - Scheduling django custom command

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.

I'm just developing an app locally using django installed on Ubunutu.

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])

This command (del_hit.py) is located in the following structure:

/project_dir
   /app
      /management
         __init__.py
         /commands
            __init__.py
            del_hit.py

As I stated, I can successfully run this command from my project_dir

python manage.py del_hit

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]})

This did execute successfully.

Finally, I tried to setup a cronjob to execute every hour

30 * * * * python /home/vadmin/development/python/my_proj/manage.py del_hit

This did not work.

I could use some assistance in getting my custom command to run on a schedule using either django-chronograph (preferably) or a simple cronjob

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It looks like you need to be in the your project directory for this to work correctly.

Try updating the command you call to do a 'cd' first,

(cd /home/vadmin/development/python/my_proj && ./manage.py del_hit)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...