how can I run celery task only once in django?

Viewed 1045
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!

2 Answers

If you are using Django-Celery-Beat, it has the ability to create tasks that run only once at a specific date/time using the ClockedSchedule model. It's not in the documentation for some reason but you can easily configure it through the Django Admin.

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

Related