Skip to content

Latest commit

 

History

History
83 lines (59 loc) · 2.74 KB

Run as a cron job example.md

File metadata and controls

83 lines (59 loc) · 2.74 KB

Steps to Run a Cron Job in a Python venv Every 5 Minutes

  1. 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.
  2. 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 in myproject.
  3. 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
  4. Make the Shell Script Executable:

    • Ensure your shell script is executable by running:

      chmod +x /path/to/your/run_my_script.sh
  5. 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

Explanation of the Cron Job Entry

  • */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.

Example

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:

  1. Shell Script (~/myproject/run_my_script.sh):

    #!/bin/bash
    source ~/myproject/venv/bin/activate
    python ~/myproject/my_script.py
    deactivate
  2. Make Shell Script Executable:

    chmod +x ~/myproject/run_my_script.sh
  3. Crontab Entry:

    crontab -e

    Add the following line:

    */5 * * * * ~/myproject/run_my_script.sh >> ~/myproject/my_script.log 2>&1

Tips for Troubleshooting

  • 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.