4

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

Ed.
  • 4,439
  • 10
  • 60
  • 78
  • Did you install it from HEAD, or a confirmed release? Have you tried swapping it out, temporarily for an older version? (See http://code.google.com/p/django-chronograph/ - which will need a DB resync as the models are slightly different) – Steve Jalim Dec 23 '10 at 15:02
  • can you change your command to not accept arguments -- maybe the issue lies in how django-chronograph passes in args? – Steve Jalim Dec 23 '10 at 15:58
  • what id /var/log/syslog or your cron log say about the failed command when you tried to run it directly via crontab? – Steve Jalim Dec 23 '10 at 16:26
  • thanks for the comments. I tried disabling the arguments. No dice. And the syslog doesn't say much: Dec 23 09:53:01 vadmin CRON[4002]: (vadmin) CMD (python /home/vadmin/development/python/my_proj/manage.py del_hit). Where do I find the cron log? – Ed. Dec 23 '10 at 16:36
  • @Ed: cron should be emailing you any output from cronjobs. If you are using roots crontab, put a line like `MAILTO=ed@acme.com` before the cron line that calls your script. – Paulo Scardine Dec 23 '10 at 20:14

2 Answers2

4

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)

aid
  • 136
  • 3
  • This works, though if someone figured out how to work django-chronograph, I'd love to hear that as well. – Ed. Dec 23 '10 at 20:04
  • ah - perhaps your PYTHONPATH doesn't include the project's root dir? – Steve Jalim Dec 23 '10 at 23:52
  • hmm. when I say: import os, os.environ['PYTHONPATH'].spplit(os.pathsep), I get ['/home/vadmin/development/python/my_proj'] – Ed. Dec 24 '10 at 16:29
2

To write a django script suitable to crontab, put this lines at the top:

#!/usr/bin/python

from django.core.management import setup_environ
import settings
setup_environ(settings)

If your file is not in the same folder of settings.py, append your app folder to pythons path.

Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • 3
    For the future readers, it's now depricated: https://docs.djangoproject.com/en/1.4/releases/1.4/#django-core-management-setup-environ – Soid Feb 23 '13 at 10:12