How to get django-cron to work automatically
Asked Answered
S

4

7

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...

Stratocumulus answered 12/4, 2013 at 22:17 Comment(1)
Did you find out the solution to this question?Diphenyl
H
2

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.

Haya answered 13/4, 2013 at 2:8 Comment(1)
Would it work if you are working in Git Bash on a Windows computer?Musick
W
1

Install django-crontab instead.

pip install django-crontab  

Change your settings.py to include django-crontab

INSTALLED_APPS = (
'django_crontab',
...
)

CRONJOBS = [
('*/5 * * * *', 'myproject.cron.my_scheduled_job')
]

create a cron.py file in your app directory

def my_scheduled_job():
   #do something

run this everytime you include or update your cron job.

python manage.py crontab add

run your local server to test your cron:

python manage.py runserver

And you are done! :)

Widera answered 7/11, 2016 at 16:49 Comment(3)
For some reason, it's not working for me. Could you help me with this?Hidalgo
I cant get this working. My job doesnt get executed. is there anything to be added?Please
why /5 ? cant we write without this ? for 1 week what we have to add ?Joashus
B
0

You can perform 'runcrons' programaticaly, like this:

from django.core.management import call_command
call_command('runcrons')

As example, i was put the above lines in my wsgi.py

Bendick answered 18/11, 2017 at 17:22 Comment(0)
P
0

Yes the management runcrons command is to run the cronjobs once. It checks the DB each time to see if individual CronJobs should be run yet. On a server in production you would have them run periodically ( like evry 5min ) by running that management command through a crontab. So you still need to write one crontab entry ( instead of many ). Go into your server under the user that runs the website ( and thus management commands ) and type:

$ crontab -e

Choose your ediitor and add an extra line that is the linux command for the django management command runscrons:

*/5 * * * * source /home/ubuntu/.bashrc && source /home/ubuntu/work/your-project/bin/activate && python /home/ubuntu/work/your-project/src/manage.py runcrons &> /home/ubuntu/cronjob.log 2>&1

This command activates bash and the virtual environment, runs the managament command and redirects stdout & stderr to a log file. Adjust the filepaths for you project as needed. This is just from the documentation: Django Cron Installation

Ploch answered 29/8 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.