How to run Cronjob in Django

Hi folks, It’s been a year since I have written a blog, but now I have got an interesting problem to solve for you guys. At the end of this tutorial, I am sure you all will be able to do things without any help. So let’s start.

In this tutorial, I am going to show you guys the way we can setup Cronjob in Django. but before going to the implementation part first, let’s understand what cronjob actually is and why we require this.

What is a Cronjob?

Cron is a useful tool that simplifies scheduling automated tasks, such as running scripts, commands, or software, on Unix-based systems. This allows users to save time and boost productivity by automating recurring tasks.

To understand this in detail let us have an example, an online retailer can set up a Cron job to generate and back up daily reports of their website traffic and sales data automatically. This not only eliminates the need for manual intervention but also minimizes the risk of data loss.

Why do we need a Cronjob?

Cronjobs are necessary to automate recurring tasks on Unix-based systems, such as executing scripts, commands, or software. By scheduling these tasks with Cron, users can eliminate the need for manual intervention and free up time. Cron offers flexibility to execute these tasks at specific dates and times or as per set intervals, such as daily, weekly, or monthly.

Aside from saving time and optimizing processes, Cronjobs can also minimize the risk of errors or omissions that may occur during manual tasks. By automating routine tasks, Cronjobs can ensure consistency and precision in their execution.

Now one of the important things about cronjob is its expression which tells us at what time the task is scheduled, so now let’s see how we can write the cronjob expression

Cronjob Expression :-  * * * * *

Now let’s understand what * describes what

First * Minute

Second  * Hour

Third  * Month Day

Fourth * Month

Fifth * Week Day

Now to understand the expression clearly let’s consider a few examples:-

To run the task at every minute we can write the expression as * * * * *

To run the task at every minute past hour 1 we can write the expression as * 1 * * *

Similarly, we can write the expressions according to our needs.

To setup Cronjob in Django we need to follow the below steps

Step 1:- First install the django-crontab module in our system using the pip

pip install django-crontab

Step 2:- After installing we need to add this our installed apps present inside the settings.py file

INSTALLED_APPS = [‘django_crontab’,…]

Step 3:- The next step is to create a file within your Django project that follows your module directory structure. For instance, let’s assume our file is called “Cronjob_script.py”. Within this file, you will define the function that you want to run automatically through cron.

Step 4:- To proceed, return to the “settings.py” file and include the following line:

CRONJOBS = [(‘* * * * *’, ‘Cronjob_script.function_name’)]

Step 5:– Execute the command to add all the defined CRONJOBS to the crontab utility (*nix). It is essential to execute this command every time a change is made to the CRONJOBS in any way.

python manage.py crontab add

Step 6:– Execute the following command to display a list of all active Cronjobs:

python manage.py crontab show

Step 7:- Execute the following command to delete all the CRONJOBS that have been defined:

python manage.py crontab remove

Now your task will run at the time you have scheduled.

So I guess this was pretty easy to understand and execute. Now you can easily set cronjob in Django using django-crontab

Thanks for your time, Have a great day

Leave a Reply

Your email address will not be published. Required fields are marked *