Search code examples
pythondjangocroncelerydjango-celery

how can I run celery task only once in django?


from __future__ import absolute_import, unicode_literals

from celery import shared_task
from celery.task import periodic_task
from celery.schedules import crontab
from datetime import timedelta



@periodic_task(run_every=(crontab(minute='*/1')), name='greeting_task')
def greeting_task():
    print('hello Dias!')

Can I create a function that runs only once at certain time with crontab? PLEASE, HELP!!! thanks in advance!


Solution

  • You need to change the parameters for crontab.

    Example: If you want the task to be run once at 5AM everyday:

    @periodic_task(run_every=(crontab(minute='0', hour='5')), name='greeting_task')
    def greeting_task():
        print('hello Dias!')
    

    crontab(minute='*/1') will run the task at every minute. Read about crontab syntax here: https://en.wikipedia.org/wiki/Cron