17

I want my custom made Django command to be executed every minute. However it seems like python /path/to/project/myapp/manage.py mycommand doesn't seem to work while at the directory python manage.py mycommand works perfectly.

How can I achieve this ? I use /etc/crontab with:

****** root python /path/to/project/myapp/manage.py mycommand
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hellnar
  • 62,315
  • 79
  • 204
  • 279

7 Answers7

22

I think the problem is that cron is going to run your scripts in a "bare" environment, so your DJANGO_SETTINGS_MODULE is likely undefined. You may want to wrap this up in a shell script that first defines DJANGO_SETTINGS_MODULE

Something like this:

#!/bin/bash

export DJANGO_SETTINGS_MODULE=myproject.settings 
./manage.py mycommand

Make it executable (chmod +x) and then set up cron to run the script instead.

Edit

I also wanted to say that you can "modularize" this concept a little bit and make it such that your script accepts the manage commands as arguments.

#!/bin/bash

export DJANGO_SETTINGS_MODULE=myproject.settings
./manage.py ${*}

Now, your cron job can simply pass "mycommand" or any other manage.py command you want to run from a cron job.

Joe Holloway
  • 28,320
  • 15
  • 82
  • 92
  • 1
    This says unknown command when I do it. I had to change it to: cd /path/to/project ./manage.py ${*} – mhost Feb 21 '10 at 19:27
  • No success. The custom command is recognized but the command apparently can't find the database – Raphael Sep 15 '11 at 18:57
  • What do you mean by "command apparently can't find the database"? Does that entail an error of some sort? – Joe Holloway Sep 16 '11 at 19:48
13
cd /path/to/project/myapp && python manage.py mycommand

By chaining your commands like this, python will not be executed unless cd correctly changes the directory.

davidfischer
  • 449
  • 3
  • 3
9

If you want your Django life a lot more simple, use django-command-extensions within your project:

http://code.google.com/p/django-command-extensions/

You'll find a command named "runscript" so you simply add the command to your crontab line:

****** root python /path/to/project/myapp/manage.py runscript mycommand

And such a script will execute with the Django context environment.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nabucosound
  • 1,283
  • 1
  • 12
  • 23
2

This is what i have done recently in one of my projects,(i maintain venvs for every project i work, so i am assumning you have venvs)

 ***** /path/to/venvs/bin/python /path/to/app/manage.py command_name

This worked perfectly for me.

Nitish Kansal
  • 478
  • 4
  • 6
2

How to Schedule Django custom Commands on AWS EC-2 Instance?

Step -1

First, you need to write a .cron file

Step-2

Write your script in .cron file.

MyScript.cron

* * * * * /home/ubuntu/kuzo1/venv/bin/python3 /home/ubuntu/Myproject/manage.py transfer_funds >> /home/ubuntu/Myproject/cron.log 2>&1

Where * * * * * means that the script will be run at every minute. you can change according to need (https://crontab.guru/#*_*_*_*_*). Where /home/ubuntu/kuzo1/venv/bin/python3 is python virtual environment path. Where /home/ubuntu/kuzo1/manage.py transfer_funds is Django custom command path & /home/ubuntu/kuzo1/cron.log 2>&1 is a log file where you can check your running cron log

Step-3

Run this script

$ crontab MyScript.cron

Step-4

Some useful command

1. $ crontab -l (Check current running cron job)
2. $ crontab -r (Remove cron job)
0
  1. If its a standalone script, you need to do this:

    from django.conf import settings
    from django.core.management import setup_environ
    setup_environ(settings)
    
    #your code here which uses django code, like django model
    
  2. If its a django command, its easier: https://coderwall.com/p/k5p6ag

    In (management/commands/exporter.py)

    from django.core.management.base import BaseCommand, CommandError
    
    class Command(BaseCommand):
        args = ''
        help = 'Export data to remote server'
    
        def handle(self, *args, **options):
            # do something here
    

And then, in the command line:

$ python manage.py exporter

Now, it's easy to add a new cron task to a Linux system, using crontab:

$ crontab -e

or $ sudo crontab -e if you need root privileges

In the crontab file, for example for run this command every 15 minutes, something like this:

# m h  dom mon dow   command
*/15 * * * * python /var/www/myapp/manage.py exporter
zengr
  • 38,346
  • 37
  • 130
  • 192
0

The runscript extension wasn't well documented. Unlike the django command this one can go anywhere in your project and requires a scripts folder. The .py file requires a run() function.