Learn how to automate your tasks by scheduling and running Python scripts with cron. In this guide, you'll understand the steps to create a Python cron job, avoid common issues with crontab, and verify that your scheduled scripts run successfully.
Prerequisites for Setting Up Python Cron Jobs
Before you start scheduling Python scripts with cron, ensure you meet the following prerequisites:
prerequisites
- Python 3 installed on your system. You can verify this by running
python3 --versionin your terminal. - Familiarity with basic Python programming.
- A Python script that you want to schedule. Create one if you don't have any.
- Access to a terminal or shell to configure cron jobs on your system.
Steps to Execute Python Scripts via Cron
Follow these steps to configure your Python script to execute at a scheduled time using cron jobs.
Create Your Python Script:
Prepare the Python script that you want to schedule. Here's an example script,
main.py, which appends the current time to a file:python# main.py from datetime import datetime with open("file.txt", "a") as f: f.write(f"Current time: {datetime.now()}\n")Wrap Python Script with a Shell Script:
Create a shell script (
script.sh) to invoke the Python script.bash#!/bin/bash python3 /path/to/main.pyReplace
/path/to/main.pywith the full file path to your Python script.Make the Shell Script Executable:
Run the following command to ensure your shell script is executable:
Make shell script executable
chmod +x /path/to/script.sh
# No output means the command was successfulLocate the Full Paths:
Use the
whichcommand to find the absolute path for Python and use it in the shell script. For example:
Find absolute path of Python
which python3
# /usr/bin/python3 Update your script.sh to use this full path.
Edit the Crontab:
Open the crontab configuration:
Edit crontab
crontab -e
# Opens the crontab for the current user in a text editorAdd the following line at the end, scheduling the job to run every minute:
* * * * * /path/to/script.sh Each * in the above cron syntax represents minute, hour, day, month, and day of the week, respectively. You can customize the schedule format as needed.
Verify the Cron Job:
Save and exit the editor, then confirm the cron job is added:
Verify crontab entries
crontab -l
# Lists active cron jobs for the current user Finally, check the output file (file.txt) after a minute to verify that the script is running correctly.
Common Pitfalls in Crontab and Python Execution
When scheduling Python scripts with cron, you might encounter these issues:
Verification of Crontab Configuration
To ensure your setup is working:
FAQ
Why is my Python cron job not running?
Ensure you have used absolute paths in your script and given appropriate permissions. Log output and errors to a file using >> /path/to/log/file 2>&1 in the crontab.
How do I schedule a Python script in a virtual environment?
Activate the virtual environment within your shell script using:
source /path/to/venv/bin/activate
python /full/path/to/your_script.pyHow do I check if my cron job worked?
You can check system logs (e.g., /var/log/syslog on Linux), output files, or custom log files where you redirect cron output.
Official reference: Python documentation.