-
Locate Your Virtual Environment:
- Ensure you know the path to your virtual environment. For example, if your virtual environment is located in your home directory under
myproject/venv
, note this path.
- Ensure you know the path to your virtual environment. For example, if your virtual environment is located in your home directory under
-
Create a Python Script:
- Write the Python script you want to run. For example, let's say your script is named
my_script.py
and is located inmyproject
.
- Write the Python script you want to run. For example, let's say your script is named
-
Create a Shell Script to Activate the Virtual Environment and Run Your Python Script:
-
Create a shell script (e.g.,
run_my_script.sh
) that activates the virtual environment and runs your Python script. Here's an example of what this shell script might look like:#!/bin/bash # Activate the virtual environment source /path/to/your/venv/bin/activate # Run the Python script python /path/to/your/my_script.py # Deactivate the virtual environment deactivate
-
-
Make the Shell Script Executable:
-
Ensure your shell script is executable by running:
chmod +x /path/to/your/run_my_script.sh
-
-
Set Up the Cron Job:
-
Edit your crontab file to schedule the cron job. Open the crontab editor with:
crontab -e
-
Add a new cron job entry to run your shell script every 5 minutes:
*/5 * * * * /path/to/your/run_my_script.sh >> /path/to/your/logfile.log 2>&1
-
*/5 * * * *
- This specifies the schedule (every 5 minutes)./path/to/your/run_my_script.sh
- The full path to your shell script.>> /path/to/your/logfile.log 2>&1
- Redirects both stdout and stderr to a log file for debugging purposes.
Here’s a complete example assuming your virtual environment is in ~/myproject/venv
, your Python script is ~/myproject/my_script.py
, and you want to log output to ~/myproject/my_script.log
:
-
Shell Script (
~/myproject/run_my_script.sh
):#!/bin/bash source ~/myproject/venv/bin/activate python ~/myproject/my_script.py deactivate
-
Make Shell Script Executable:
chmod +x ~/myproject/run_my_script.sh
-
Crontab Entry:
crontab -e
Add the following line:
*/5 * * * * ~/myproject/run_my_script.sh >> ~/myproject/my_script.log 2>&1
- Check the Paths: Ensure all paths (to the virtual environment, the Python script, and the log file) are correct.
- Log Output: Always redirect output to a log file to capture any errors or output for debugging.
- Environment Variables: If your script relies on environment variables, make sure to set them in the shell script or within the cron job entry.