Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get django-cron to work automatically

I am trying to get django-cron working and its not. I followed the instruction here to set up my cron but the problem is that my job only runs when i type python manage.py runcrons on my command line and the job is not run every 5 minutes. I don't know what else to do. I have read other documents on crontabs and chronograph but am confused. Do I install crontabs along with cron or chronograph or will cron work fine with only django-cron. Also how do I get my job to run automatically. In the documentation here I read Now everytime you run the management command python manage.py runcrons all the crons will run if required. Depending on the application the management command can be called from the Unix crontab as often as required. Every 5 minutes usually works for most of my applications. . What does this mean. What am I missing here. Am lost. HELP

Settings.py

CRON_CLASSES = (
"myapp.views.MyCronJob",
)

INSTALLED_APPS = (

'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_cron',

'django.contrib.admin',
'django.contrib.admindocs',
'myapp',


)

views.py

from django_cron import CronJobBase, Schedule
class MyCronJob(CronJobBase):
RUN_EVERY_MINS = 10 # every 10 min

schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'my_app.my_cron_job'    # a unique code

def do(self):
    print "10 min Cron"
    theJob()

I should mention that am using pycharm on a windows platform to run django...

like image 972
flexxxit Avatar asked Apr 12 '13 22:04

flexxxit


People also ask

Do cron jobs run automatically?

Cron reads the crontab (cron tables) for predefined commands and scripts. By using a specific syntax, you can configure a cron job to schedule scripts or other commands to run automatically.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week . 0 1 * * * - this means the cron will run always at 1 o'clock.


1 Answers

The root of your problem leads to operating system. Webserver is not that kind of deamon that calls your cronjobs, it just handels web requests. To call periodic tasks on windows you need to use Windows Task Scheduler:

What is the Windows version of cron?

Other way to solve your problem is to start celery deaemon in in celery beat mode.

http://celeryproject.org/

http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html

This is a harder way, if you have very simple application you don't need to use celery. But there are many cases when queues are the best solution.

like image 70
singer Avatar answered Sep 20 '22 06:09

singer