<p>This tutorial is just to demonstrate how to use crontab to schedule a task in Django. So I am taking a simple django&nbsp;<a href="https://github.com/prime7/django_cron_tutorial" target="_blank">project</a>&nbsp;and focus more on information to automate with crontab.</p><p>There are many ways to use crontab to automate a python script. If we use virtualenv then we have to activate our environment first then run the script in crontab. But there is an easy process that comes with Django, custom commands. We will make a script to run the task in django then make a scheduler to run it periodically.</p><blockquote class="blockquote"><p>Custom Commands in Django</p></blockquote><p>We can create our own custom commands for our apps and include them in the list by creating a <b>management/commands </b>directory inside an app directory, like below. See sample&nbsp;<a href="https://github.com/prime7/django_cron_tutorial" target="_blank">here</a>.&nbsp;</p><p>The name of the command file will be used to invoke using the command line utility. For example, if our command was called 'my_command.py' , then we will be able to execute it via:</p><pre>python manage.py my_command</pre><p>To learn more about custom commands in django see this beautiful&nbsp;<a href="https://simpleisbetterthancomplex.com/tutorial/2018/08/27/how-to-create-custom-django-management-commands.html" target="_blank">tutorial</a>.</p><p><br></p><blockquote class="blockquote"><p>Running the custom command from a bash script</p></blockquote><p>Create a bash script file with extension sh like&nbsp;</p><pre>!#/bin/bash python venv/bin/activate python manage.py runserver</pre><p>Make it executable by&nbsp;</p><pre>chmod +x filename.sh</pre><p>Now check it from terminal by typing the below code</p><pre>bash filename.sh</pre><p>Now, It's time to create crontab command. I<span style="font-size: 1rem;">n terminal type</span></p><pre><span style="font-size: 1rem;">crontab -e or sudo crontab -e</span></pre><p>You can use this&nbsp;<a href="https://crontab-generator.org/" target="_blank">tool</a> to generate your crontab command. Now you are all set to make the scheduler. Use this beautiful&nbsp;<a href="https://medium.com/@gavinwiener/how-to-schedule-a-python-script-cron-job-dea6cbf69f4e" target="_blank">tutorial</a>&nbsp;<span style="font-size: 1rem;">to run your crontab command.</span></p><hr><p><span style="font-size: 1rem;">Special note </span></p><p><span style="font-size: 1rem;">Use full path in both crontab and your bash script. Bash script will work individually without full path but to work with corntab give full path.</span></p><p><a href="https://github.com/prime7/django_cron_tutorial" target="_blank">Link</a><span style="font-size: 1rem;">&nbsp;for this tutorial.<br></span><br></p>

About the author

Tarek

My goal is to excel in advanced computer applications in order to contribute to the organization as well as to self-efficacy and communication skills to build my career. I like programming challenges to solve under pressure.