2
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!

dusembekd
  • 25
  • 3

2 Answers2

1

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.

MaestroFJP
  • 366
  • 1
  • 8
0

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

yanxun
  • 566
  • 2
  • 10
  • 1
    not everyday, only once at 5:00 for example, and only today, not tomorrow and so one, only once! Is it possible? – dusembekd Apr 13 '20 at 06:19
  • Ah I see, this link might help. https://stackoverflow.com/questions/41568305/celery-run-task-once-at-a-specified-time – yanxun Apr 13 '20 at 07:08