0

When I run python manage.py runcrons, the cron job successfully executes (prints 'Executed'). But it doesn't do it automatically. It's supposed to execute every minute as I've stated in the Class. Here's the code:

settings.py

CRON_CLASSES = [
    "app.views.MyCronJob"
]

app.views

class MyCronJob(CronJobBase):
    RUN_EVERY_MINS = 1

    schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
    code = 'my_app.my_cron_job'    # not sure what this is supposed to be?

    def do(self):
        print('Executed')

Any idea?

Zorgan
  • 8,227
  • 23
  • 106
  • 207

2 Answers2

0

you also need to set up the crontab entry for the cron:

> crontab -e
*/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

taken from official documentation

Ayush
  • 3,695
  • 1
  • 32
  • 42
  • I'm running mac osx not Ubuntu will this be different for me? So my project from desktop is `project/bin/activate`. – Zorgan Sep 24 '17 at 00:44
  • I guess you'll have to install & configure one of the packages that provides cron (fcron, cronie, etc) and configure that – Ayush Sep 24 '17 at 00:46
0

+1 for Ayush's answer. This worked for me.

crontab -e

This will open the editor, copy paste the following line at the bottom and save & close.

*/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

In addition to this try rerunning these in your Django project directory:

python manage.py runcrons --force
service cron start
Abhay
  • 46
  • 1
  • 9