2

I have a cron which I have written using django-cron:

from django_cron import CronJobBase, Schedule

class MyCronJob(CronJobBase):
    RUN_EVERY_MINS = 1

    schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
    code = 'statuscheck.my_cron_job'

    def do(self):
       print ("hello")

It works, as when the command python manage.py runcrons is run twice in a row, only one output is provided unless the 1 minute has lapsed

My question is, how do i then schedule that command to be executed for example 3 times a week?

Thank you

CodingDuck
  • 60
  • 2
  • 9

2 Answers2

2

Just change the number of minutes, given there are 60 minutes in an hour, 24 hours in a day and 7 days in a week, you could do something like :

RUN_EVERY_MINS = 60 * 24 * 7 / 3 # three times a week

About the issue mentioned in your comment, I think you should use CRON.

https://en.wikipedia.org/wiki/Cron

You could add this line in /etc/crontab (replace /path/to by real path) :

* * * * * cd /path/to/manage.py && /usr/bin/python manage.py runcrons

This will run your script every minute.

Note that you won't see "hello" on your terminal's session. If you want to see the CRON output you could redirect it to a file :

* * * * * cd /path/to/manage.py && /usr/bin/python manage.py runcrons >> /tmp/django_cron.log
Loïc
  • 11,804
  • 1
  • 31
  • 49
  • 1
    Hi @Loïc, thank you for taking your time to reply :) When I run the cron, it only executes once, it doesn't execute every minute. What the application currently does it executes once and if I try to execute the same command again, it doesnt work unless 1 minute has lapsed - do i need another scheduling program to run my python manage.py runcrons ` ? – CodingDuck Oct 09 '17 at 14:55
  • 1
    Hi @theMicroGirl I've edited my answer to answer your comment. – Loïc Oct 09 '17 at 15:01
-2
  1. One thing to do crontab is using django celery.
  2. Do the setup in celery_config.py, Write all the configuration settings in this file Example:

    from celery.schedules import crontab CELERY_BEAT_SCHEDULE = CELERY_BEAT_SCHEDULE = { 'client-gets-email-on-new-message': { 'task': 'apps.app_name.tasks.send_email', 'schedule': crontab(minute="*") }, }

  3. Finally configuration settings.py file

    INSTALLED_APPS = ( 'django_celery_results', 'django_celery_beat' )