Skip to content

Scheduling a Python Script with Crontab

Steps
4
Category
Python
Covers
Cron
Versions & support
About Python →
On this page 4 steps

In the world of programming, efficiency is key. Automation is one of the most powerful tools in a developer’s toolkit, allowing for the seamless execution of tasks without manual intervention. For Python developers, one of the most accessible and effective methods to automate scripts is through Crontab, a time-based job scheduler in Unix-like operating systems. This article provides a comprehensive guide to scheduling Python scripts using Crontab, ensuring that your tasks are executed efficiently and reliably.

Step 1 of 4Writing Your Python Script

Before scheduling a task, you need a Python script. Write your Python script to perform a specific task. For instance, a script could scrape data from a website, process a file, or send an automated email. Make sure to test your script thoroughly to ensure it runs without errors.

Script Example:


import datetime
print(f"This script ran at {datetime.datetime.now()}")

I have saved above script in /home/tecadmin/app/cron.py.

Step 2 of 4Set Execution Permissions

For Crontab to execute your Python script, the script must have execution permissions. Use the following command to grant execution permissions:

chmod +x /home/tecadmin/app/cron.py 

Step 3 of 4Schedule Python Script with Crontab

To schedule your Python script, you need to edit the crontab file. Type `crontab -e` in your terminal. This will open the crontab file in your default text editor.

To configure a Python script, choose one of the following methods based on your setup:

  • Using the Default Python Version: If the script runs with the Python version that comes installed by default on the system, apply this crontab configuration:
    
    */15 * * * * python /home/tecadmin/app/cron.py
    
    
  • Using Specific Python Version: If a specific Python version, other than the system’s default, is required, specify the full path to that Python binary. This is useful for scripts that need a specific Python version not set as the default on the system:
    
    */15 * * * * /usr/bin/python3.10 /home/tecadmin/app/cron.py
    
    
  • Python in a Virtual Environment: For scripts that operate within a Python virtual environment, schedule them as follows, where /home/tecadmin/app/venv is the directory of the virtual environment:
    
    */15 * * * * /home/tecadmin/app/venv/bin/python /home/tecadmin/app/cron.py
    
    

After you’ve entered your scheduling command, save and exit the crontab editor. Crontab will automatically install the new crontab file. You can read more about scheduling jobs with crontab using this and this tutorial.

Step 4 of 4Checking Your Cron Jobs

To ensure that your cron job is scheduled, you can list your cron jobs with:

crontab -l 

Troubleshooting Tips

If your script doesn’t run as expected, check the following:

  • Ensure your script works outside of cron.
  • Check your script’s execution permissions.
  • Verify the path to the Python interpreter.
  • Ensure the script’s path in the cron job is correct.
  • Review cron logs for errors, typically found in /var/log/syslog or /var/log/cron.

Conclusion

Automating Python scripts using Crontab is a powerful way to schedule regular tasks efficiently. By following the steps outlined in this guide, you can ensure your scripts run reliably and automatically, freeing up valuable time to focus on more complex and creative aspects of your programming projects. Happy coding and automating!

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

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


Back to top