The crontab could be a list of commands simply need to run on a regular schedule, and also the name of the command used to manage that list.
cron is the system process which will automatically perform tasks for you according to a set schedule. The schedule is called the crontab, which is also the name of the program used to edit that schedule.
Note : 1) dead simple crontab powered job scheduling for django (1.8+).
2) Crontab will not work with windows.
Installation
To install crontab in project, need to run this command:
pip install django-crontab
Add it to installed app
After installed, add it to installed apps in your django project settings.py
:
INSTALLED_APPS = ( 'django_crontab', ... )
Create new method
now create a new method that should be executed by cron every 5 minutes, i.e. in myapp/cron.py:
def my_crontab(): pass
Add method in setting.py
After define your logic in method, now add this to your settings.py
:
CRONJOBS = [ ('*/5 * * * *', 'myapp.cron.my_crontab') ]
Some Cron Expression Examples
Cron Expression | Meaning |
---|---|
* * * * * 2020 | Execute a cron job every minute during the year 2020 |
* * * * * | Execute a cron job every minute |
*/5 * * * * | Execute a cron job every 5 minutes |
0 * * * * | Execute a cron job every hour |
0 12 * * * | Fire at 12:00 PM (noon) every day |
15 10 * * * | Fire at 10:15 AM every day |
15 10 * * ? | Fire at 10:15 AM every day |
15 10 * * * 2020-2022 | Fire at 10:15 AM every day during the years 2020, 2021 and 2022 |
* 14 * * * | Fire every minute starting at 2:00 PM and ending at 2:59 PM, every day |
0/5 14,18 * * * | Fire every 5 minutes starting at 2:00 PM and ending at 2:55 PM, AND fire every 5 minutes starting at 6:00 PM and ending at 6:55 PM, every day |
0-5 14 * * * | Fire every minute starting at 2:00 PM and ending at 2:05 PM, every day |
10,44 14 * 3 3 | Fire at 2:10 PM and at 2:44 PM every Wednesday in the month of March. |
15 10 * * 1-5 | Fire at 10:15 AM every Monday, Tuesday, Wednesday, Thursday and Friday |
15 10 15 * * | Fire at 10:15 AM on the 15th day of every month |
15 10 L * * | Fire at 10:15 AM on the last day of every month |
15 10 * * 5L | Fire at 10:15 AM on the last Friday of every month |
15 10 * * 5#3 | Fire at 10:15 AM on the third Friday of every month |
0 12 1/5 * * | Fire at 12:00 PM (noon) every 5 days every month, starting on the first day of the month. |
11 11 11 11 * | Fire every November 11th at 11:11 AM. |
11 11 11 11 * 2020 | Fire at 11:11 AM on November 11th in the year 2020. |
0 0 * * 3 | Fire at midnight of each Wednesday. |
0 0 1,2 * * | Fire at midnight of 1st, 2nd day of each month |
0 0 1,2 * 3 | Fire at midnight of 1st, 2nd day of each month, and each Wednesday. |
you can also define positional and keyword arguments which let you call django management commands:
CRONJOBS = [ ('*/5 * * * *', 'myapp.cron.other_cron_job', ['arg1', 'arg2'], {'verbose': 0}), ('0 4 * * *', 'django.core.management.call_command', ['clearsessions']), ]
Run all defined cronjobs
finally run this command to add all defined jobs from CRONJOBS to crontab:
python manage.py crontab add
show current active jobs of this project:
python manage.py crontab show
removing all defined jobs is straight forward:
python manage.py crontab remove
Note : Run python manage.py crontab add
each time you change CRONJOBS in any way!
You can see more configuration setting in this link.
That is it for today, hope it helps. If you have a better approach to resolve this problem please make a comment in comment section below.
—
If you like this article, you can buy me a coffee. Thanks!
Everything is very open with a very clear description of the challenges. It was really informative. Your site is very helpful. Thank you for sharing!